Use of AppendDataBoundItems of DropDownList or Specify item 0 in DropDownList as select an item

There are three examples are given below.

AppendDataBoundItems="true"

The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items.
The value of this property is stored in view state.

Ex 1)
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="LocationName"
DataValueField="LocationName" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text=" Select an item..." Value=" Select an item..."></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:inventoryConnectionString %>"
SelectCommand="SELECT [LocationName] FROM [LocationTable]">
</asp:SqlDataSource>
Ex 2)
You can use DataBound Event to insert the item in 0th location before databounded from database.

protected void DropDownList1_DataBound(object sender, EventArgs e)
{
DropDownList1.Items.Insert(0, "Select an item...");
}

Ex 3)
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="LocationName"
DataValueField="LocationName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:inventoryConnectionString %>"
SelectCommand="SELECT [LocationName] FROM [LocationTable]">
</asp:SqlDataSource>

In Code behind

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Select Item"));

}
}
Ebook Download
View all
Learn
View all