Validating list controls in ASP.NET using RequiredFieldValidator

RequiredFieldValidator control can be used to validate ASP.NET list controls like DropDownList, ListBox and RadioButtonList so that they cannot be left blank. Users must select a value from these list controls when validation has been applied. To validate a list control, we need to set InitialValue property of the RequiredFieldValidator along with other properties. Let us see how to validate these controls

DropDownList Example:

<asp:DropDownList ID="ddlCountry" runat="server">
     <asp:ListItem Value="-1" Text="Select Country"></asp:ListItem>
     <asp:ListItem Value="1" Text="India"></asp:ListItem>
     <asp:ListItem Value="2" Text="Australia"></asp:ListItem>
     <asp:ListItem Value="3" Text="UK"></asp:ListItem>
     <asp:ListItem Value="4" Text="USA"></asp:ListItem>
     <asp:ListItem Value="5" Text="Canada"></asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator
     ID="rfvCountry"
     runat="server"
     ErrorMessage="(Required)"
     ControlToValidate="ddlCountry"
     InitialValue="-1"
     ForeColor="Red"
     BackColor="Yellow">
</asp:RequiredFieldValidator>

ListBox Example:

<asp:ListBox ID="lbState" runat="server">
     <asp:ListItem Value="1" Text="Rajasthan"></asp:ListItem>
     <asp:ListItem Value="2" Text="Bihar"></asp:ListItem>
     <asp:ListItem Value="3" Text="Delhi"></asp:ListItem>
     <asp:ListItem Value="4" Text="Kerala"></asp:ListItem>
     <asp:ListItem Value="5" Text="Jammu & Kashmir"></asp:ListItem>
</asp:ListBox>

<asp:RequiredFieldValidator
     ID="rfvState"
     runat="server"
     ErrorMessage="(Required)"
     ControlToValidate="lbState"
     InitialValue=""
     ForeColor="Red"
     BackColor="Yellow">
</asp:RequiredFieldValidator>

RadioButtonList Example:

<asp:RadioButtonList ID="rblCity" runat="server">
     <asp:ListItem Value="1" Text="Patna"></asp:ListItem>
     <asp:ListItem Value="2" Text="Gaya"></asp:ListItem>
     <asp:ListItem Value="3" Text="Bhagalpur"></asp:ListItem>
     <asp:ListItem Value="4" Text="Muzaffarpur"></asp:ListItem>
     <asp:ListItem Value="5" Text="Nalanda"></asp:ListItem>
</asp:RadioButtonList>

<asp:RequiredFieldValidator
     ID="rfvCity"
     runat="server"
     ErrorMessage="(Required)"
     ControlToValidate="rblCity"
     InitialValue=""
     ForeColor="Red"
     BackColor="Yellow">
</asp:RequiredFieldValidator>

In all the list controls above, we have set InitialValue property of the ValidationControls to the default value of the controls they validate. For DropDownList control it is set to “-1” i.e., value of the default (first) element of the DropDownList. InitialValue of the corresponding RequiredFieldValidator for ListBox and RadioButtonList controls are set to blank (“”), which specifies that no element is selected from these lists.

Note: CheckBoxList control cannot be validated using RequiredFieldValidator

Following validation errors will be displayed when the Submit button is clicked without selecting any options from the controls

Validation of List Controls in ASP.NET

Complete HTML:

<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<style type="text/css">
input.textbox
{
border: 1px solid #BABABA;
color: #212121;
font-family: "Segoe UI Light";
font-size: 100%;
padding:4px 8px;
width:200px;
height:22px;
margin-bottom:5px;
}

input.button
{
background-color: Navy;
color: #FFFFFF;
font-family: "Segoe UI Light";
border: 0 none;
font-size: 110%;
height: 2.142em;
min-width: 6em;
line-height: 142%;
width:100px;
margin-top:5px;
}

label
{
color: #606060;
float: left;
font-family: "Segoe UI";
font-size: 85%;
padding-bottom: 4px;
padding-top: 10px;
text-align: left;
width:150px;
}

.clear
{
clear:both;
}

.dropdown
{
width:200px;
height: auto;
padding-bottom: 4px;
padding-top: 4px;
}

.radiobuttonlist
{
width:200px;
height: auto;
border: 1px solid Navy;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<div>
<asp:Label
ID="lblCountry"
runat="server"
Text="Country"
AssociatedControlID="ddlCountry">
</asp:Label>
</div>

<div style="clear:both;"></div>

<div>
<asp:DropDownList ID="ddlCountry" runat="server" CssClass="dropdown">
<asp:ListItem Value="-1" Text="Select Country"></asp:ListItem>
<asp:ListItem Value="1" Text="India"></asp:ListItem>
<asp:ListItem Value="2" Text="Australia"></asp:ListItem>
<asp:ListItem Value="3" Text="UK"></asp:ListItem>
<asp:ListItem Value="4" Text="USA"></asp:ListItem>
<asp:ListItem Value="5" Text="Canada"></asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator
ID="rfvCountry"
runat="server"
ErrorMessage="(Required)"
ControlToValidate="ddlCountry"
InitialValue="-1"
ForeColor="Red"
BackColor="Yellow">
</asp:RequiredFieldValidator>
</div>

<div class="clear"></div>

<div>
<asp:Label
ID="lblState"
runat="server"
Text="State"
AssociatedControlID="lbState">
</asp:Label>
</div>

<div style="clear:both;"></div>

<div>
<asp:ListBox ID="lbState" runat="server" CssClass="dropdown">
<asp:ListItem Value="1" Text="Rajasthan"></asp:ListItem>
<asp:ListItem Value="2" Text="Bihar"></asp:ListItem>
<asp:ListItem Value="3" Text="Delhi"></asp:ListItem>
<asp:ListItem Value="4" Text="Kerala"></asp:ListItem>
<asp:ListItem Value="5" Text="Jammu & Kashmir"></asp:ListItem>
</asp:ListBox>

<asp:RequiredFieldValidator
ID="rfvState"
runat="server"
ErrorMessage="(Required)"
ControlToValidate="lbState"
InitialValue=""
ForeColor="Red"
BackColor="Yellow">
</asp:RequiredFieldValidator>
</div>

<div class="clear"></div>

<div>
<asp:Label
ID="lblCity"
runat="server"
Text="City"
AssociatedControlID="rblCity">
</asp:Label>
</div>

<div style="clear:both;"></div>

<div>
<div style="float:left;">
<asp:RadioButtonList ID="rblCity" runat="server" CssClass="radiobuttonlist">
<asp:ListItem Value="1" Text="Patna"></asp:ListItem>
<asp:ListItem Value="2" Text="Gaya"></asp:ListItem>
<asp:ListItem Value="3" Text="Bhagalpur"></asp:ListItem>
<asp:ListItem Value="4" Text="Muzaffarpur"></asp:ListItem>
<asp:ListItem Value="5" Text="Nalanda"></asp:ListItem>
</asp:RadioButtonList>
</div>

<div style="float:left; padding-left:5px;">
<asp:RequiredFieldValidator
ID="rfvCity"
runat="server"
ErrorMessage="(Required)"
ControlToValidate="rblCity"
InitialValue=""
ForeColor="Red"
BackColor="Yellow">
</asp:RequiredFieldValidator>
</div>
</div>

<div class="clear"></div>

<div>
<asp:Button
ID="btnSubmit"
runat="server"
Text="Submit"
CssClass="button" />
</div>
</div>
</form>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *