Thursday, March 22, 2012

Setting the dropdowns to no selection

In the form load event I am binding data to several different dropdowns. I need to set the dropdowns to no selection.

I attempted to try this but it fails:

With dropdown1
.DataSource = datareader
.DataTextField = "desc"
.DataValueField = "id"
.DataBind()
.SelectedIndex = -1
End With

So I did this:

With dropdown1
.DataSource = datareader
.DataTextField = "desc"
.DataValueField = "id"
.DataBind()
.Items.Insert(0, "")
.SelectedIndex = 0
End With

But the problem with the second part is that when I put code in the "IsPostBack" section to select an item from the dropdown I get an error telling me that only 1 item can be selected at a time.

If Page.PostBack = False Then
dropdown1.Items.FindByValue(CType(datareader("id"), String)).Selected = True
End IfAre you opening the datareader again on the postback? The datareader is gone from the first time you open it.
yes I am opening another datareader under the postback to return back a single item that was selected previously and saved in the database. I want to go to the selected item in the dropdown and select it.

0 comments:

Post a Comment