ASP.NET - DropDown DataBinding (Rebind?)
I have a drop down which has a method which binds data to it:
dropDown.Items.Clear()
dropDown.AppendDataBoundItems = True
Select Case selType
Case SelectionTypes.Empty
dropDown.Items.Insert(0, New ListItem("", ""))
Case SelectionTypes.Any
dropDown.Items.Insert(0, New ListItem("ANY", ""))
Case SelectionTypes.Select
dropDown.Items.Insert(0, New ListItem("Select One", ""))
End Select
BindDropDown(val)
The BindDropDown method simply sets the datasource, datakeyname, datavaluename, and then databinds the data.
For a reason which I cannot avoid, I MUST call this method twice sometimes. When it is called twice, All of the databound items show up two times, but the top item (the one I manually insert) is ther开发者_JAVA百科e only once.
Is ASP doing something wierd when i databind twice even though i clear the list between? Or does it have to do something with the viewstate/controlstate?
EDIT
The entire page, and this control has EnableViewState="false"
EDIT 2
The dropdown is inside a form view. After the selected value is set I have to rebind the dropdown just in case the selected value is not there [because it is an inactive user]. After this, the formview duplicates the databound items.
Here is what was happening: My dropdown control binds the data when the selected index is set. So it sets the DataSource to be my table of values and it calls DataBind.
When the formview is bound, it sets the selected index which binds my dropdown HOWEVER after this happens the formview calls DataBind() on ALL of its children. And since my dropdown has AppendDataBoundItems = "True" it adds the items in the DataSource again to the dropdown.
My solution:
SET DataSource to be null after I call DataBind() in the SelectedIndex Property. This way when the Formview calls DataBind() it appends no items and therefore I have no more duplicates.
Try doing it this way:
dropDown.Items.Clear()
BindDropDown(val)
Select Case selType
Case SelectionTypes.Empty
dropDown.Items.Insert(0, New ListItem("", ""))
Case SelectionTypes.Any
dropDown.Items.Insert(0, New ListItem("ANY", ""))
Case SelectionTypes.Select
dropDown.Items.Insert(0, New ListItem("Select One", ""))
End Select
精彩评论