I have a wbdropdown control that I have bound in the code behind. When I try to set a selected item so that it is the item that is displayed in the text box I use this code.
Me.myDDL.FindItemByValue("x").Selected = True
Where x is set to the correct value. I get an error stating that the object is not set to a vlaue. Using a watch I can see that the items collection is empty, but if I let the code run the drop down list is populated.
If I set the binding at design time I don't the issue where the object is not set to a value. But I don't get the text displaying in the text field.
I am looking for any way to set the text field of the drop down with an item from the list.
Thanks in advance.
you should put that code in the control's DataBound event handler, to ensure that when you do it, the data binding has taken place.
Hope it helps,
Angel
Thanks for the response
I added the code below to the code behind. There is a fair amount of logic behind what is going to be displayed and building the query, so I put this logic there.
The value displayed was still the first item in the table, not the item that is referenced by the code below. I am pulling the data into a table and binding that. I can view the items in the drop down list and verify that the value that I am looking for is there.
Protected Sub TestDDLHandle() Handles ddlX.DataBound Me.ddlX.Items.FindItemByValue("123").selected = True End Sub
The answer is to iterate through the items collection and find the value required and then set the currentvalue and the selected. After setting both of those to the item that matched the value everything works properly.