Validating Drop-Down Lists with the RequiredFieldValidator Control


Validating Drop-Down Lists with the RequiredFieldValidator Control:

So far, you have seen a lot of examples of using the RequiredFieldValidator control with a series of text boxes, but you can just as easily use this validation control with other form elements as well.

For example, you can use the RequiredFieldValidator control with an <asp:DropDownList> server control. To see this, suppose that you have a drop-down list that requires the end user to select her profession from a list of items. The first line of the drop-down list includes instructions to the end user about what to select, and you want to make this a required form element as well.

 

Drop-down list validations:

<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Selected="True">Select a profession</asp:ListItem>
<asp:ListItem>Programmer</asp:ListItem>
<asp:ListItem>Lawyer</asp:ListItem>
<asp:ListItem>Doctor</asp:ListItem>
<asp:ListItem>Artist</asp:ListItem>
</asp:DropDownList>
&nbsp;
<asp:RequiredFieldValidator id="RequiredFieldValidator1"
runat="server" ErrorMessage="Please make a selection"
ControlToValidate="DropDownList1"
InitialValue="Select a profession">
</asp:RequiredFieldValidator>

Other Related Posts:

Validations:

ASP.NET Validation Server Controls

The RequiredFieldValidator Server Control in asp.net

Validating Drop-Down Lists with the RequiredFieldValidator Control

The CompareValidator Server Control in asp.net

The RangeValidator Server Control in asp.net

The RegularExpressionValidator Server Control in asp.net

The CustomValidator Server Control in asp.net

The ValidationSummary Server Control in asp.net

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s