Uğur UMUTLUOĞLU
Web Technologies, Microsoft and .NET Technologies

Inserting Item to DataBounded DropDownList

Thursday, 11 December 2008 09:57 by ugur

In the conditions that we load the records to DropDownList control from the database, all of us want the first choice of the DropDownList control a user informative text. I am sure; it is a necessity for every application to display the first item like “Choose a city”, “Choose a category” etc. choices and then list other items coming from the database. Actually, it is not a logic way to add a “Choose a city” record to the Cities table in the database; thus; we must create a different solution way for this issue! If we want to solve this issue in SQL Server side, UNION terms can be used. However, in this post, I want to discuss another different solution way.      

Actually, this process is a very simple and useful solution way. First, we add the informative text to the DropDownList control as a ListItem from the designer part. We add the informative text to the items collection and then we change the AppendDataBoundItems property of the DropDownList as true. At last, the added choice will be listed as a DropDownList control item.

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"

    DataSourceID="SqlDataSource1" DataTextField="SehirAdi" DataValueField="SehirId">

    <asp:ListItem Value="-1">Bir şehir seçin</asp:ListItem>

</asp:DropDownList>

AppendDataBoundItems

 This is an alternative way for UNION and UNION ALL terms and we should keep this in our minds as a hint…

 
Add to Technorati Favorites