Friday, March 28, 2008
Posted on Friday, March 28, 2008 10:05:07 AM (Mountain Daylight Time, UTC-06:00)  Comments [0] | 
Categories: ASP.NET

Data binding is a really handy way to fill list controls with values from some data source - usually a database, but it can come from almost anywhere. However, there are times when you may want your list box to have some additional items - maybe an instructional message - "Please pick a value..." or an "All" option.

So - just a quick reminder that you can do this very very easily in ASP.NET.

<asp:DropDownList ID="cboCountry"
           runat="server" Width="250px"
           DataTextField="CountryName"
           DataValueField="CountryId"
           AppendDataBoundItems="true">
     <asp:ListItem Value="-1">All</asp:ListItem>
</asp:DropDownList>

Just set AppendDataBoundItems to true, and add in your other values as asp:ListItem entries. In my case the drop down list allows the user to filter data by country. But I also needed a way to have them remove a country filter - i.e. show all the data again. Thus the "All" option, and the -1 value.

Comments are closed.