Please enable Javascript to correctly display the contents on Dot Net Tricks!

Validate ListBox and DropDown List using Required Field Validator

  Author : Shailendra Chauhan
Posted On : 17 Jun 2012
Total Views : 45,882   
Updated On : 24 Sep 2016
 

In Asp.net, sometimes we need to validate ListBox and DropDown List using Required field validator. It is tricky and avoids the use of JavaScript function to validate ListBox and DropDown List. Here I am sharing the code.

Validate DropDown List and ListBox

 <form id="form1" runat="server">
<div>
 <asp:ListBox ID="lstboxCity" runat="server" Width="200px" SelectionMode="Multiple">
 <asp:ListItem Value="1">Delhi</asp:ListItem>
 <asp:ListItem Value="2">Noida</asp:ListItem>
 <asp:ListItem Value="3">Gurgaon</asp:ListItem>
 <asp:ListItem Value="4">Mumbai</asp:ListItem>
 <asp:ListItem Value="5">Pune</asp:ListItem>
 <asp:ListItem Value="6">Banglore</asp:ListItem>
 <asp:ListItem Value="7">Lucknow</asp:ListItem>
 </asp:ListBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="lstboxCity" InitialValue="" runat="server" ErrorMessage="Please select atleast one city"></asp:RequiredFieldValidator>
</div>
<br />
<div>
 <asp:DropDownList ID="ddlCity" runat="server" Width="200px">
 <asp:ListItem Value="0">--Select--</asp:ListItem>
 <asp:ListItem Value="1">Delhi</asp:ListItem>
 <asp:ListItem Value="2">Noida</asp:ListItem>
 <asp:ListItem Value="3">Gurgaon</asp:ListItem>
 <asp:ListItem Value="4">Mumbai</asp:ListItem>
 <asp:ListItem Value="5">Pune</asp:ListItem>
 <asp:ListItem Value="6">Banglore</asp:ListItem>
 <asp:ListItem Value="7">Lucknow</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="ddlCity" InitialValue="0" runat="server" ErrorMessage="Please select city"></asp:RequiredFieldValidator>
 </div>
 <br />
 <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
 </form> 

Note

For validating DropDown List, make sure you have "--Select--" option value 0 other wise validation on it will not work.

Summary

In this article I try to explain how to validate DropDownList and ListBox using Required Field Validator. I hope after reading this article you will be able to use this trick in your code. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.



Free Interview Books
 
COMMENTS (1)
android developer jobs in bangalore Reply

.NET is the superb platform for dump high-end applications, and there are a runs like the wind code of ways to what end programmers can use.NET to create solid and reusable applications. This passage discusses seven lucky books that finance developers to raw material various features of .NET public opinion research development.

3 APR
Master Class Fast Track MVC 5 with Angular2 Development (Online)

10:30 AM -03:30 PM IST (Fast Track)

Know More
1 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

Know More
1 APR
ASP.NET MVC with Angular2 Development (Classroom)

4:00 PM-5:30 PM

Know More
25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

6 MAR
ASP.NET MVC with Angular2 Development (Online)

07:00 AM - 09:00 AM IST( MON, WED, FRI)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

05:00 PM-06:30 PM

31 JAN
ASP.NET MVC with Angular2 Development (Online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

30 JAN
NODEJS & MEAN Stack 2.x Development (Online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

31 DEC
ASP.NET MVC with AngularJS Development (Classroom)

09:30 AM-11:00 AM

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 

Professional Speaks

+