ARTICLE

How to validate DropDownList control in ASP.NET using VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET July 18, 2011
Here, we will learn how to validate a DropDownList in ASP.NET.
 
Reader Level:

You can use RequiredFieldValidator control with InitialValue as the default value for the DropDownList box. You can also validate the selected value of ASP. Net DropDownList control using RequiredFieldValidator, when user clicks the list item of dropdownlist control to select it. A good way of using the InitialValue property is with the DropDownList server control.

For example

a drop-down list might have a default value that is not an empty value (by default, some text appears in it). An example of a drop-down list with the RequiredFieldValidator that uses the without InitialValue property is illustrated in Below code.

ASPX PAGE:

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:dropdownlist id="dropDowncolor" runat="server">

    <asp:ListItem Text="Select Color" Value="none" />

        <asp:ListItem Text="Red" Value="Red" />   

        <asp:ListItem Text="Blue" Value="Blue" />   

        <asp:ListItem Text="Green" Value="Green" /> 

</asp:dropdownlist>

       <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="dropDowncolor" ErrorMessage="Please select color"></asp:RequiredFieldValidator>

<p>

<asp:button id="btnSubmit" runat="server" text="Submit" onclick="SubmitData" />

</p>

     </div>

    </form>

</body>

</html>

Now run the application

ddl1.gif

DropDownList1

Now using InitialValue property in the RequiredFieldValidator control.

<div>

    <asp:dropdownlist id="dropDowncolor" runat="server">

    <asp:ListItem Text="Select color" Value="0" />

    <asp:ListItem Text="red" Value="18" />

    <asp:ListItem Text="blue" Value="19" />

    <asp:ListItem Text="pink" Value="20" />

    <asp:ListItem Text= "green" Value=">20" />

</asp:dropdownlist>

<asp:requiredfieldvalidator id="reqcolor" runat="server" errormessage="Please select color" forecolor="Red" controltovalidate="dropDowncolor" initialvalue="0" />

<p>

   <asp:button id="btnSubmit" runat="server" text="Submit" onclick="SubmitData" />

</p>

 </div>

 

Now run the application and click on the Button control.

ddl2.gif

DropDownList2

Now select item from the DropDownList.

ddl3.gif

DropDownlist3

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor