Question

this has been driving me mad... I have two dynamically created asp dropdown boxes, makes and models. When a user selects the makes, it calls an ASP button called makeChanged. When makeChanged runs, it is supposed to clear the items in the models drop down list and then create new ones. So the code for handling the makeChanged button is as follows:

Sub makeChanged (ByVal sender As Object, ByVal e As System.EventArgs)

Dim thingID = sender.ID.split("_")(1)

Dim modelDD As DropDownList = thingsContainer.FindControl("model_" & thingID )
modelDD.EnableViewState = False
modelDD.Items.Clear()
modelDD.SelectedValue = -1
modelDD.AppendDataBoundItems = True

Dim select_qry = "select * from Models where makeid=" & sender.SelectedValue

Dim objData2 As SqlDataReader = getSqlQueryObj(select_qry)
modelDD.DataSource = objData2
modelDD.DataValueField = "models"
modelDD.DataTextField = "models"
modelDD.DataBind() 'This is where the error is getting triggered from

End Sub

I know for a fact there is nothing wrong with the query or finding the drop down because i have ran many response.write's and it comes back with the expected outcome. I thought that it might be due to the fact that viewState was preserving the old value on index changed and then trying to add it to the list after i re-populated the list with new values from the DB however i turned off viewState to check this and it still doesnt make a difference :|.

Any help is appreciated! Thank you!

Was it helpful?

Solution

You have set the SelectedValue to -1 which must exist. Don't confuse it with the SelectedIndex where -1 indicates that nothing should be selected.

So i assume that you actually want:

' ...
 modelDD.Items.Clear()
 modelDD.SelectedIndex = -1  ' instead of SelectedValue = -1
' ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top