DataBound ListBox

A

anastasia

I have the following code (this is a snippet):

InitDBConn();
OpenDBConnection();

OleDbCommand cmdRidge = new OleDbCommand("SELECT * FROM
TRDRidgeTypes", oleDbConn);

OleDbDataReader oleRidges= cmdRidge.ExecuteReader();

if (!this.IsPostBack)
{
ddlRidge.DataSource = oleRidges;
ddlRidge.DataMember = "TRDRidgeTypes";
ddlRidge.DataTextField = "RidgeType";
ddlRidge.DataValueField = "RidgeTypeID";
ddlRidge.DataBind();
}


----
This code successfully fills a DropDownList box. However, when the
user selects one of the items and the page is posted to the server
,the SelectedItem property of the List is null and the SelectedIndex i
(-1). I am tearing my hair out because I have found people on the
newsgroup with a similar
problem, but it was due to their not setting data source in the
"this.PostBack =False" condition. I have done that, and it still does
not help me.

I do have ViewState for the control set to True. I tried hardcoding
the ListItems in the HTML and the control values were read correctly.

I am sure I am missing the obvious here..but please help!!

Thanks,
Stacey
 
D

David Waz...

You don't want to bind the data on post-back.
You are wiping out the results when you bind the data!

only bind when postback is TRUE, or when you know the
content will/should be changing. Otherwise, the viewstate
will handle everything else for you automatically.

email if you need more...
 
J

Jim Blizzard [MSFT]

Stacey is not binding on the postback--at least not in the code below.
Notice the ! (not) in the IF statement. The code shown says to do the
binding if it's not a postback.

We need to see more of the code. What else happens if it IS a postback?
Do you manually re-populate the dropdownlist if it is a postback. If
that's the case, then that's the problem.

-bliz

--
Jim Blizzard
Sr .NET Developer Evangelist
Microsoft

Your Potential. Our Passion.

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only, so that others may benefit. Thanks.


--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top