[ACCEPTED]-DropDownList AppendDataBoundItems (first item to be blank and no duplicates)-append

Accepted answer
Score: 65

Instead of using AppendDataboundItems='true' (which will cause the 4 problem you are talking about), respond 3 to the DataBound event for the DropDownList and then add your 2 "blank" item to the top of the list.

<asp:DropDownList runat="server" ID="MyList"
  ondatabound="MyListDataBound"></asp:DropDownList>

Then 1 in your code behind:

protected void MyListDataBound(object sender, EventArgs e)
{
    MyList.Items.Insert(0, new ListItem("- Select -", ""));
}
Score: 6

There are good answers here but I felt the 16 need to include more information because 15 there are multiple options that work and 14 we need to decide which to use.

First, we 13 should understand AppendDataBoundItems. If AppendDataBoundItems = "true", ListItems are added to the 12 DropDownList without clearing out the old ones. Otherwise, the 11 DropDownList is cleared about before the next DataBind. MSDN AppendDataBoundItems doc

There 10 are basically 2 options covered by most 9 of the answers:

1. Define a blank option in html and add the ListItems from the database to the DropDownList only once.

Notice 3 things here:

  • Blank ListItem is defined in html
  • AppendDataBoundItems="true"
  • DataBind is NOT called on postbacks or when the DropDownList item count is > 1

Source:

<asp:DropDownList ID="MyList" runat="server" AppendDataBoundItems="true" DataValueField="Id" DataTextField="Name" >
    <asp:ListItem Text="- Select One -" Value="" />
</asp:DropDownList>

Code 8 behind:

protected void Page_Load(object sender, System.EventArgs e)
{
    if (MyList.Items.Count <= 1 ) {
        MyList.DataSource = MyDataSource;
        MyList.DataBind();
    }
}

Note: I like the logic of checking 7 the count vs checking IsPostBack. Though PostBacks 6 are often the cause of duplicate databinding, it 5 is possible to cause it other ways. Checking 4 the item count is basically just checking 3 to see if it's already been loaded.

OR (option 2 to use IsPostBack instead)

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack) {
        MyList.DataSource = MyDataSource;
        MyList.DataBind();
    }
}

2. Clear and reload the DropDownList on each page refresh.

Notice 3 differences from 1 the first option:

  • AppendDataBoundItems="false" (if it is not defined then false is it's default value)
  • Blank ListItem is is added in code behind. We can't define it in html because with AppendDataBoundItems="false", it would be cleared out.
  • DataBind is called on every Page_Load

Source:

<asp:DropDownList ID="MyList" runat="server" DataValueField="Id"  DataTextField="Name" 
    OnDataBound="MyList_DataBound" >
</asp:DropDownList>

Code behind:

protected void Page_Load(object sender, System.EventArgs e)
{
    MyList.DataSource = MyDataSource;
    MyList.DataBind();
}

protected void MyList_DataBound(object sender, EventArgs e)
{
    MyList.Items.Insert(0, new ListItem("- Select One -", ""));
}
Score: 4

You probably bind that DropDownList in the 2 code behind. So you should not do it after 1 postback again:

// probably in Page_Load method
if (!Page.IsPostBack)
{
    // do data binding here
};
Score: 3

Here is an idea, we can use 2 events: DataBound and 1 DataBinding:

protected void MyListDataBound(object sender, EventArgs e)
{
  MyList.Items.Insert(0, new ListItem("- Select -", ""));
}

protected void MyListDataBinding(object sender, EventArgs e)
{
  MyList.Items.Items.Clear();
}
Score: 0
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"
  DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">
    <asp:ListItem Text="(Select a State)" Value="" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
  ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
  SelectCommand="SELECT DISTINCT [state] FROM [authors]">
</asp:SqlDataSource>

0

Score: 0

Here's an Idea.

There's a property in the 4 drop down list called AutoPostBack set it to true and 3 then in the code behind you put all the 2 binding method inside the if(!Page.IsPostBack). That worked 1 for me.

regards.

More Related questions