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"));
}
}
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.
Abdul SamiPosted May 16, 2018, 1:37 AM
This video on youtube will help you https://www.youtube.com/watch?v=iaiKHi7aUmw
Ravi ShekharPosted Apr 11, 2013, 2:17 AM
this property in not available in windows for ComboBox.