Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an initial "select" value to a DropDownList

If I use a DropDownList:

 <asp:DropDownList ID="DropDownListSubContractors" runat="server" 
     DataTextField="Company_Name" DataValueField="id">
 </asp:DropDownList>

What attribute do I use/set that allows me to use '---Select---' as initial option on the drop down, instead of the first value in the list.

like image 897
John Avatar asked Feb 04 '13 12:02

John


1 Answers

You can use

<asp:DropDownList ID="DropDownListSubContractors" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id">
    <asp:ListItem Text="---Select---" Value="0" />   
</asp:DropDownList>

Or you can add this dynamically at code behind like this

DropDownListSubContractors.Items.Add(new ListItem("---Select---", "0"));
like image 152
nrsharma Avatar answered Sep 17 '22 15:09

nrsharma