mmcglynn mmcglynn - 1 year ago 149 Question

Databound drop down list - initial value

How do I set the initial value of a databound drop down list in ASP.NET?

For instance, I want the values, but the first value to display should be -- Select One ---, with a null value.

Answer Source

I think what you want to do is this:

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Text="--Select One--" Value="" />   

Make sure the 'AppendDataBoundItems' is set to true or else you will clear the '--Select One--' list item when you bind your data.

If you have the 'AutoPostBack' property of the drop down list set to true you will have to also set the 'CausesValidation' property to true then use a 'RequiredFieldValidator' to make sure the '--Select One--' option doesn't cause a postback.

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download