DropDownList AppendDataBoundItems Problem

So, I want a DropDownList that lets you pick a name, and then when you do it puts it in a FormView so you can look at it, edit it, or delete it. There’s also a New button in the FormView for adding new names.

Well, at first it works great. But when I add a new name, it doesn’t automatically show up on my DropDownList immediately, and when I delete a name, it doesn’t go away immediately. If I leave the page and come back, the DropDownList is up-to-date.

Aha! I think, and go into the code behind and add a handler or two for the DataSource the FormView is pointing to. It says, when the FormView is used to add or Delete someone, to DataBind the DropDownList.

So I go and test it. I add a name with FormView. The DropDownList DataBinds again. And the result is that the new list of names is appended to the old list of names, so everyone is in there twice. I delete a name with FormView. The DropDownList DataBinds again, and again appends the new list to the old, so now everyone is in there three times. Obviously, I don’t want to append the new list to the old one, I want the new list to replace the old one. I looked for a method to clear the old list, so I could clear it and then DataBind. Couldn’t find one.

I have AppendDataBoundItems set to “true” so that I could make the first item in the DropDownList say “–Please Choose A Name–”. This is the source of the trouble. In my code behind, if I set this to “false” right before I DataBind, then the DropDownList stops getting longer. So that’s good. But now “–Please Choose A Name–” is no longer an option.

When someone is deleted, I need that “–Please Choose A Name–” option to be there and be selected. Right now, when one person is deleted, it selects the first person on the list, which is unhelpful and confusing.

When someone is added, I need for their name to be selected. I know I can set the DropDownList.SelectedValue property, but I don’t know what to set it to.

    Protected Sub ObjectDataSourceSalespeople_Deleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSourceSalespeople.Deleted
        DropDownList1.AppendDataBoundItems = False
    End Sub

    Protected Sub ObjectDataSourceSalespeople_Inserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceStatusEventArgs) Handles ObjectDataSourceSalespeople.Inserted
        DropDownList1.AppendDataBoundItems = False
        DropDownList1.SelectedValue = e.????
    End Sub

The DropDownList:

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
        DataSourceID="ObjectDataSourceNamesAndUsernames" AppendDataBoundItems="true"
        DataTextField="SalespersonFullName" DataValueField="PKtxtSalespersonUserName">
            <asp:ListItem Text="--Please Choose a User--"></asp:ListItem>

If anyone can offer any advice, or code, or help, I’d greatly appreciate it.

Use DropDownList1.Items.Clear before you bind. Once you bind, add this code:

Dim item As New ListItem
item.Text = "--Please Choose A Name--"
item.Selected = True
DropDownList1.Items.Insert(0, item)

That will insert an item at the top of the dropdown the the text “–Please Choose A Name–” in it selected

Awesome! You make it look easy. I can’t thank you enough.