8

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
John
  • 3,965
  • 21
  • 77
  • 163
  • I noticed comments about making sure the "--Select--" ListItem isn't selected. Here's how to require a different selection: http://stackoverflow.com/a/38727154/3347858 – Tony L. Aug 02 '16 at 18:57

3 Answers3

15

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"));
nrsharma
  • 2,532
  • 3
  • 20
  • 36
  • Sorry about the delay in replying, but thanks for the answer. The program still runs if the user hasnt chosen anything from the drop down but still presses submit, how can I fix it so the user needs to select a value from the drop down before they cna click the button?Or they can still click the button but the code won't run and an error will display please select... – John Feb 04 '13 at 15:09
  • You can use either required or CustomValidator to prevent user to select the value from drop down. You can also validate this by javascript like if(document.getElementById("DropDownListSubContractors").value==0){alert('Please select value');}. – nrsharma Feb 05 '13 at 05:05
7
<asp:DropDownList ID="DropDownListSubContractors" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id">
    <asp:ListItem Text="---Select---" Value="" />   
</asp:DropDownList>

You could now bind your DropDown in the code behind as usual to the datasource and this data source doesn't need to contain a default value item:

IEnumerable<MyViewModel> model = ...
DropDownListSubContractors.DataSource = model;
DropDownListSubContractors.DataBind();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Sorry about the delay in replying, but thanks for the answer. The program still runs if the user hasnt chosen anything from the drop down but still presses submit, how can I fix it so the user needs to select a value from the drop down before they cna click the button? Or they can still click the button but the code won't run and an error will display please select... – John Feb 04 '13 at 15:10
  • You could use assign a Required validator to the dropdown: http://stackoverflow.com/questions/2280559/how-to-add-a-requiredfieldvalidator-to-dropdownlist-control – Darin Dimitrov Feb 04 '13 at 15:13
2

You can do this this way:

From Code behind:

DropDownListSubContractors.Items.Insert(0, new ListItem("---Select---", string.Empty));

Note: We use index 0 to make it the first element at the list.

Harold Javier
  • 887
  • 2
  • 7
  • 16
  • Sorry about the delay in replying, but thanks for the answer. The program still runs if the user hasnt chosen anything from the drop down but still presses submit, how can I fix it so the user needs to select a value from the drop down before they cna click the button? Or they can still click the button but the code won't run and an error will display please select... – John Feb 04 '13 at 15:11
  • John, consider adding a RequiredFieldValidator so the user won't be able to click the submit button without choosing from the selections – Harold Javier Feb 04 '13 at 15:19