Add Default ListItem to a DropDownList Control
Posted by Tihomir Ivanov on 10 November 2009 13:19
Rating: 0.00
Sometimes it's usefult to add default ListItem to a DropDownList Control (ex. '[none]' or '[empty]'),
You can do it in two ways:
1st Way -Programmatically:
_yourDropDownList.Items.Insert(0, new ListItem("[none]", "some default value");
The first parameter of the Insert Method is the index where to insert the item, it's very useful because you can insert the default item after loading the other items.
2nd Way Using theDropDownList's AppendDataBoundItems Property:
You can add it into your aspx page
<asp:DropDownList ID="_yourDropDownList" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text="[none]" Value="some default value" />
</asp:DropDownList>
That's all.
Comments:
No comments yet.