views:

179

answers:

2

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 there 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___

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.

A: 

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
bechbd
I cannot add the value after, because when I try to set the SelectedValue = "", it says that it is not in the list of items, but when I use AppendDataBoundItems and do it before the databind, it lets me set the SelectedValue = "".
Bob Fincheimer
A: 

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.

Bob Fincheimer