What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 15687 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > ASP.NET Validations Tips and Tricks Part-I

ASP.NET Validations Tips and Tricks Part-I

2 vote(s)
Rating: 5 out of 5
Article posted by Sheonarayan on 7/15/2011 | Views: 3664 | Category: ASP.NET | Level: Intermediate | Points: 250 red flag


In this article we shall learn how to validate a TextBox as a mandatory field, how to validate a DropDownList / ListBox and What is Cause Validation property and how to avoid validation on clicking of a button.

Introduction

Validation controls are used to validate the ASP.NET input server controls like asp:TextBox for valid data when their data is submitted to the server. There are six validation controls in ASP.NET and they are

1. RequiredFieldValidator

2. RangeValidator

3. CompareValidator

4. RegularExpressionValidator

5. CustomValidator and

6. ValidationSummary

Get hundreds of ASP.NET, ADO.NET, jQuery, HTML, CSS and JavaScript How to Tips and Tricks, click here to get them.

Now, let's learn some Tips and Tricks related with Validation controls.

How to validate a TextBox as a mandatory field?

To make a TextBox mandatory for the end user to enter before a form can be submitted to the server, we can follow this approach.

ASPX PAGE

 

Username:<asp:TextBox ID="txtUserName" runat="server" />

<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !" ForeColor="Red"ControlToValidate="txtUserName" />

<p><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" /></p>

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{


}

In the above code snippet, we have a TextBox and a Button. As we want to validate the textbox as a mandatory field so we have used asp:RequiredFieldValidator with ErrorMessage that we want to display if the validation fails and ControlToValidate property set to the TextBox (in this case the textbox id is txtUserName) that should be validated.

OUTPUT


How to validate a DropDownList / ListBox?

To validate a DropDownList or ListBox as a mandatory field so that user has to select any item from them apart from pre-selected item, we can follow this approach.

ASPX PAGE

Select age: <asp:DropDownList ID="dropDownAge" runat="server">

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

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

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

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

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

</asp:DropDownList>

<asp:RequiredFieldValidator ID="reqAge" runat="server" ErrorMessage="Please

select age" ForeColor="Red" ControlToValidate="dropDownAge" InitialValue="0" />

<p><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="SubmitData" /></p>

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{


}

In the above code snippet, the first item of the DropDownList is “Select Age” with value as “0”; we want the user to select any age. To do that we can specify the RequiredFieldValidator's InitialValue as “0”, this will cause the RequiredFieldValidator fail unless user select any age other than “0” from the DropDownList.

OUTPUT

Note: Similarly we can perform ListBox validation too.


What is CauseValidation property and how to avoid validation on clicking of a button?

In case we have multiple buttons on the page and we want to avoid validation on clicking of certain button, we can use this approach.

ASPX PAGE

<p>

Username: <asp:TextBox ID="txtUserName" runat="server" />

<asp:RequiredFieldValidator ID="req1" runat="server" ErrorMessage="Mandatory !"

ForeColor="Red"ControlToValidate="txtUserName" />

</p>

<p>

Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />

<asp:RequiredFieldValidator ID="req2" runat="server" ErrorMessage="Mandatory !"

ForeColor="Red"ControlToValidate="txtPassword" />

</p>

<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="SubmitData" />

 

<asp:Button ID="Button1" runat="server" Text="Do something else"

CausesValidation="false"OnClick="DoSomethingElse" />

CODE BEHIND

protected void SubmitData(object sender, EventArgs e)

{

}


protected void DoSomethingElse(object sender, EventArgs e)

{

}

 

In the above code snippet, on .aspx page we have Username and Password two textboxes. We have placed RequiredFieldValidator for both of them. Clicking “Login” button causes validation on both TextBoxes but clicking on “Do something else” button doesn't cause form validations because its CauseValidation property is set to false.

OUTPUT


Hope this article was useful. Thanks for reading !!!

Do let me know your feedback about the article and series of articles that has been published and going to publish, this encourages us to do better to help you understand clearly.

Subscribe to RSS feed to get the forthcoming articles alert directly into your inbox.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

>> Write Response - Respond to this post and get points
Related Posts

Read ConnectionString Information from Application Configuration File.

This FAQ is like a starter kit. It will help you understand the main aspects of Ajax in a rapid fashion....

The easiest way to make a redirection in ASP.NET is using Response.Redirect(url). What it actually does is, that it creates a response with the "302 (Object Moved)" status code and the target destination. It tells the browser that the requested page is temporarily moved to a new location and then the browser makes a request to the new destination. If the page is permanently moved, then the 302 status code is no longer correct. Search engines also looks at 301 and 302 redirects differently. Here's a quote from The Internet Digest: There is no natural way of doing a 301 redirect in ASP.NET, so you have to set the HTTP headers manually. Below given is a small method that illustrates how to do it. All you have to do is to call it from the Page_Load or preferably from Page_Init or in ASP.NET 2.0 Page_PreInit.

In this article i will show you how to use sitemap navigational control in asp.net

To do custom pagination in the GridView or to do SEO friendly pagination for the GridView, we can follow this approach.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 7:41:51 AM