Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

DropDownList problem

Hi. I have never posted to any kind of forum before because I have always been able to find the answers eventually, but I am completely stumped by this problem.

I'm populating a dropdownlist from a database.
I populate the DropDownList only
if (!Page.IsPostBack). It all works great, but when I select from the dropdownlist and then attempt to set the text value of a textbox based on the selecteditem by pressing a button
For example:
private void Button1_Click(object sender, System.EventArgs e)
  TextBox3.Text = DropDownList1.SelectedItem.Text;
it just doesn't work.

When I code the same thing but use a DropDownList where the items are added at design-time, it works fine. I have searched high and low and cannot figure out what I'm doing wrong.
As mentioned earlier, I'm only doing the databinding if !Page.IsPostBack so it's not that.
Any suggestions? Please? Wah.

Naomi Bridge
Thursday, August 28, 2003

Have you disabled ViewState either for the DropDownList or for the page?

If you don't enable ViewState, your only other option is to take out the check for the postback and always execute the database query (or pull the data from session or application state or the application cache) to fill the dropdown list.

Thursday, August 28, 2003

Hi. Thanks for responding. Yes, the EnableViewState is set to true.

I'm just starting to clue in that my problem is related to the fact that my DataValueField in the dropdownlist are set to values some of which are null. When I changed them all to unique values, it seems to work okay. I don't get that at all.

I would think DropDownList1.SelectedItem.Value would return the DataValueField contents of the selected item regardless of what's in there. But it's almost like dropdownlist uses the DataValueField contents as an index... ?

Naomi Bridge
Thursday, August 28, 2003

How are you populating the dropdown?  There may be a problem there that could easily be overlooked.

Greg Hurlman
Friday, August 29, 2003


I see what you mean (or at least I can reproduce it).  I think it's because of the way HTML works.

If you look at the HTML source for the ListBox it uses (no suprise here) the <option value=""> HTML tag.  When you submit the form, the attribute value is posted back to the server... not the text associated with the list item.  Because you have a list with duplicate values the only option for the DropDownList ASP.NET web control is to select the first list item with that value.

Does any of that make sense? ;)

Friday, August 29, 2003

That should be "because of the way HTML and HTTP works"...

Friday, August 29, 2003

Maybe a dumb question but have you set the property


It took me a while before I had figured that one out

Geert-Jan Thomas
Monday, September 1, 2003

Yes, GuyIncognito, that makes perfect sense. Looks like I'll have to use a different db column for the DataValueField and do a DB read on SelectedIndexChanged to get the info I really want to populate the other TextBox (not ideal). Thanks for the input.

Naomi Bridge
Tuesday, September 2, 2003

*  Recent Topics

*  Fog Creek Home