Use RequiredFieldValidator to validate drop down list

By Jatin Prajapati

Hi, In this FAQ we will see the user of RequiredFieldValidator to validate drop down list.

Refer the following code example.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
        
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Required Field Validator to validate drop down list</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btn" runat="server" Text="OK" />
        <br />
        DDL1: <asp:DropDownList ID="ddl1" runat="server">
            <asp:ListItem Text="Select" Selected="True"></asp:ListItem>
            <asp:ListItem Text="01Dept1"></asp:ListItem>
            <asp:ListItem Text="02Dept2"></asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="rfv" runat="server" ControlToValidate="ddl1" InitialValue="Select"
        
ErrorMessage="Please select from drop down!!!"></asp:RequiredFieldValidator>
    </div>
    </form>
</body>
</html>

Use RequiredFieldValidator to validate drop down list  (2156 Views)