I recently had a DropDownList being pre-populated from a database. I also wanted to insert "Select" at the top of the list and make it a required field. Googling this yielded much misinformation for what turned out to be pretty simple to solve in the end.

Say you have a DDL like this:

<asp:DropDownList id="testDDL" runat="server">
    <asp:ListItem Text="Select"></asp:ListItem>
</asp:DropDownList>

 

If we try to populate this DropDownList from a database with the values 1,2 and 3, and then try and add a RequiredFieldValidator, that validator will not fire.

Some people resort to creating a CustomValidator and/or JavaScript here. This is not necessary. We need to take a step back here and re-phrase the problem: We already have data in our list in the shape of the "Select" item. We now want to append data from the database. A quick search of the MSDN documentation leads to the AppendDataBoundItems property for the DropDownList.

From the documentation: "The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items." The italics are mine...

We can use the InitialValue of the RequiredFieldValidator in conjunction with the AppendDataBoundItems property of the DropDownList to arrive at a working solution:

<asp:DropDownList id="testDDL" AppendDataBoundItems="true" runat="server">
    <asp:ListItem Text="Select"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="salaryReqVal" ControlToValidate="salaryDDL"
    ErrorMessage="*" Display="dynamic" InitialValue="Select"
        runat="server"></asp:RequiredFieldValidator>




I recently had to implement a pair of cascading DropDownLists for an ASP.NET Web project and found very little workable code samples on Google. Here's the code that worked for me.

The second DropDownList is enabled based on the selection made in the first and the cascade works in both directions, depending on the selection. The tricky bit was getting the index of the selection in the second drop down. I eventually found samples of the IndexOf and FindByText ListItemCollection methods on the MSDN site.

    <asp:DropDownList ID="InstrumentDDL" AutoPostBack="true"  
        OnSelectedIndexChanged="InstrumentDDL_SelectedIndexChanged"
            Width="100" runat="server">
        <asp:ListItem Text="Select" Value="Select"></asp:ListItem>
        <asp:ListItem Text="Guitar" Value="Guitar"></asp:ListItem>
        <asp:ListItem Text="Mandolin" Value="Mandolin"></asp:ListItem>
    </asp:DropDownList>

    <asp:DropDownList ID="GuitarDDL" AutoPostBack="true"  
        OnSelectedIndexChanged="GuitarDDL_SelectedIndexChanged"
            Enabled="false" Width="100" runat="server">
        <asp:ListItem Text="Select" Value="Select"></asp:ListItem>
        <asp:ListItem Text="Fender" Value="Fender"></asp:ListItem>
        <asp:ListItem Text="Gibson" Value="Gibson"></asp:ListItem>
        <asp:ListItem Text="Gretsch" Value="Gretsch"></asp:ListItem>
        <asp:ListItem Text="Martin" Value="Martin"></asp:ListItem>
    </asp:DropDownList>

    protected void InstrumentDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Reset GuitarDDL
        GuitarDDL.SelectedIndex = -1;

        // Dynamically enable the GuitarDDL based on InstrumentDDL selection
        if (InstrumentDDL.SelectedValue == "Guitar")
        {
            GuitarDDL.SelectedItem.Text = "Select";
            GuitarDDL.Enabled = true;
        }
        else
        {
            GuitarDDL.SelectedItem.Text = GuitarDDL.SelectedValue;
            GuitarDDL.Enabled = false;
        }
    }

    protected void GuitarDDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Every menu item refers to "Guitar" in the InstrumentDDL menu (except "Select")
        if (GuitarDDL.SelectedValue == "Select")
        {
            // Reset InstrumentDDL menu and disable the GuitarDDL menu
            InstrumentDDL.SelectedIndex
                = InstrumentDDL.Items.IndexOf(InstrumentDDL.Items.FindByText("Select"));
            GuitarDDL.Enabled = false;
        }
        else
        {
            InstrumentDDL.SelectedIndex
                 = InstrumentDDL.Items.IndexOf(InstrumentDDL.Items.FindByText("Guitar"));
        }
    }