How to validate a DropDownList in asp.net

DropDownList Validation
DropDownList is an asp.net list web server control that allows us to select a single item from a drop-down list at a time. DropDownList control contains ListItem objects. The following asp.net c# example code demonstrates to us how can we validate a DropDownList control in a web form.

When users submit a form without changing the selection of DropDownList items, then the first item is posted to the server as a selected item if there is no initial selected item. Normally web developers put an instruction-only item at the DropDownList items top such as an item with the text 'select an item' etc. So web developers need to ensure that users select an item from DropDownList control other than the first item. We can validate a DropDownList server control using the RequiredFieldValidator control.

RequiredFieldValidator is an asp.net validation server control that makes the associated input control a required field. We can make the DropDownList input control a required field by attaching it with a RequiredFieldValidator control.

To do this we need to set the RequiredFieldValidator ControlToValidate property value to DropDownList control's ID which dropDropDownList downlist control we want to validate. RequiredFieldValidator control's InitialValue property gets or sets the initial value of the associated input control. In this example code, the DropDownList initial value is 'Choose One', because we set this text for the DropDownList control's first item Text property value. The initial value is only used for information only, users need to select any one item from the DropDownList control other than it.

RequiredFieldValidator ErrorMessage property gets or sets the text for the error message displayed in a ValidationSummary control or the RequiredFieldValidator control itself when validation fails.
DropDownListValidation.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        Label1.Text = "Your selected item is : " +
            DropDownList1.SelectedItem.Text.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to validate a DropDownList in asp.net</title>
</head>
<body style="padding:25px">
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">      
            DropDownList Validation
        </h2>      
        <hr width="450" align="left" color="Gainsboro" />
        <asp:Label
            ID="Label1"
            runat="server"
            Font-Bold="true"
            Font-Names="Comic Sans MS"
            ForeColor="ForestGreen"
            Font-Italic="true"
            Font-Size="X-Large"
            />
        <br /><br /><br />
        <asp:DropDownList 
            ID="DropDownList1" 
            runat="server"
            Width="350"    
            Font-Size="X-Large"    
            Font-Names="Comic Sans MS"
            ForeColor="MidnightBlue"
            BackColor="FloralWhite"
            >
            <asp:ListItem Selected="True">Choose One</asp:ListItem>
            <asp:ListItem>BulletedList</asp:ListItem>
            <asp:ListItem>Button</asp:ListItem>
            <asp:ListItem>Calendar</asp:ListItem>
            <asp:ListItem>DataGrid</asp:ListItem>
            <asp:ListItem>DataList</asp:ListItem>
            <asp:ListItem>DataPager</asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator
             ID="RequiredFieldValidator1"
             runat="server"
             ControlToValidate="DropDownList1"
             InitialValue="Choose One"

             ErrorMessage="* Please select an item."
             ForeColor="Red"
             Font-Names="Impact"
             >
        </asp:RequiredFieldValidator>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Text="Validate DropDownList" 
            OnClick="Button1_Click"
            Font-Bold="true"
            Font-Size="Large"
            ForeColor="DodgerBlue"
            Font-Names="Monaco"
            Height="45"
            Width="350"
            />
    </div>
    </form>
</body>
</html>