Setting DropDownList selectedItem from database values

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I'm loading a dropdownlist control from values in a
database.... after I load the drop down, I need to set
the selecteditem to another database value... when I do
this the drop down has an additional value added to the 0
position rather than setting the proper value.
 
You need to set the selected SelectedIndex property. A way
to do this if you don't know which item it is at design
time is the following:

'If your item is a string object.
dim tmpListItem as System.Web.UI.WebControls.Listitem
tmpListItem = ddlMyDropDownList.FindByText("StringValue")
'Make sure it found what you were looking for.
if not tmpListItem is Nothing then
ddlMyDropDownList.SelectedIndex = _
tmpListItem.Items.IndexOf(tmpListItem)
end if

Hope this helps.
 
Back
Top