Help - DropDownList Blues

  • Thread starter Thread starter Frank Rocco
  • Start date Start date
F

Frank Rocco

Hello,

I have a DropDownList that I populate from an odbc datasource in code.
When I bring up the form for editing, how do I select the value in the
DropDownList based on a value stored in the database?

I tried:
lb1.SelectedItem.Text = dr["field"];
lb1.SelectedItem.Value = dr["field"];

Questions:
1. What happens if the item store in the database is no longer in the
dropdownlist box?
2. How can I sync two dropdownlist boxes?

When dropdownlist1 is selected, I fill dropdownlist2 when values from a
database based on the selected value in dropdownlist1.
When I bring the form up to edit a record, I want to select the value in
dropdownlist2 that matches the value in the database. This is not working
for me. The events are firing and the dropdownlists are filled, but the
value is not selected in the database.

Hope I explained this ok.

Thanks
Frank
using c#
 
how do I select the value in the DropDownList based on a value stored in
the database

ListItem li = ddYourList.Items.FindByValue("YourValue");
if (li != null)
li.Selected = true;

Be careful you do not try and set two listitems' selected property to true
at the same time, or you will get an error.
 
Thanks again James,

Is it better to do it your way (method 1) over method 2 for robustness?
method 1
ListItem li = lbROOM_BED.Items.FindByValue(dr["ROOM_BED"].ToString());
if (li != null)
li.Selected = true;

method 2
lbROOM_BED.SelectedIndex =
lbROOM_BED.Items.IndexOf(lbROOM_BED.Items.FindByText(dr["ROOM_BED"].ToString
()));
 

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

Back
Top