tags:

views:

43

answers:

2

Hello, I have a dropdownlist that is being populated from a database. That is working fine. The nonselectvalue of the control is -1 and the nonselectlabel is "..". When I try to open an existing record for editing, I can't seem to select the correct row. There are three parts to the ID: 111A-DD-12345. When the record is returned from the database, the ID is parsed into the three fields. The first and third parts are textboxes on the page, but the middle part is a dropdownlist. When we open an existing record, we parse out the ID with the following code:

string[] chunks = cID.Split('-');
ddOffice.SelectedItem.Text = chunks[1];

But this just changes the first row of the ddl to DD (using the example ID above), so we end up with two rows in the ddl that have the same displayed text. How do I programmatically set the dropdownlist to the correct value?

Edit: We also tried ddOffice.SelectedValue = chunks[1] , but that just displays the .. of the default, non-selected row.

TIA, Theresa

+1  A: 

Try this:

ddOffice.SelectedIndex = ddOffice.Items.IndexOf(ddOffice.Items.FindByText(chunks[1]));
Matthew Jones
I have always found working with the index far more reliable than using the text value of the box. Text handling can be wonky.
joshlrogers
Thank you, Matthew! That worked perfectly.
Theresa
A: 

(Edited) You're setting the Text property of the current SelectedItem and not setting the SelectedItem itself. Depending on how your data is sourced, set the SelectedIndex or SelectedValue instead.

Stuart Dunkeld
Yeah, I realized that after I posted the question.
Theresa
Isn't it always the way..
Stuart Dunkeld