Frustrating dropdownlist problem!!
I am rendering a dropdownlist to a page dynamically in Page_INIT... i.e
Code:
Dim typeDropDown As New DropDownList
typeDropDown.ID = "typeDropDown1"
typeDropDown.DataValueField = "ItemID"
typeDropDown.DataTextField = "ItemName"
Page.Controls.Add(typeDropDown)
I then bind my data to the dropdownlist...
Code:
PopulateDropdown(typeDropDown, objDataReader("datatable").ToString())
Now in Page_Load(), I need to set the selected item in the dropdown from the database.
But setting it manually, using
Code:
Dim typeDropDown As New DropDownList
typeDropDown = Page.FindControl("typeDropDown1")
typeDropDown.Items.FindByText("Choice 2").Selected = True
Gives me the error...
A dropdownlist cannot have multiple items selected!!!
Now why is this happening? I am only setting the selected item the first time in Page_Load(), there are no postbacks or anything on the page. This doesn't make any sense to me. Please help!!!
Re: Frustrating dropdownlist problem!!
Ok, this is fixed, quite strange fix though.
Instead of binding to the dropdown in Page_Init, (as I have always done), I moved the binding to Page_Load() in the same routine where the listitem is selected.