Search This Blog

Saturday, September 20, 2008

How to add a default ListItem to a DropDownList

This can be done one of 2 ways. A default ListItem can be added to a DropDownList programmatically with the following syntax after binding data to the DropDownList:


//Code here to populate DropDownList

DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value")

This will add a ListItem to index 0, which will be the first ListItem.

In .NET 2.0, this can be done declaratively using the AppendDataBoundItems property. This will append all data-bound ListItems to the DropDownList, leaving those you add manually as the first selections.




Solution by : Ryan Olshan
ASPInsider | Microsoft MVP, ASP.NET
http://ryanolshan.com

No comments: