What is "CauseValidation" property and how to avoid validation on clicking of a button? How to validate two different sets of Form fields separately?

SheoNarayan
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 8236 red flag
Rating: 4 out of 5  
 2 vote(s)

In scenario where a page has multiple buttons and the need is to avoid validation on clicking of certain button, we can follow this approach.

Validations

Validation controls are used to validate the form fields for valid data when it is submitted to the server.

Get 500+ ASP.NET web development Tips & Tricks and ASP.NET Online training here.

ASP.NET provides 5 validation types (and controls) out of the box. They are

1. RequiredFieldValidator

2. RangeValidator

3. CompareValidator

4. RegularExpressionValidator and

5. CustomValidator

A related control i.e ValidationSummary doesn't participate in validation but used alongwith other validation controls to display the error messages from all other controls, together as a summary.

In the previous article, we learnt about How to validate a TextBox,DropDownList , ListBox as a mandatory field? In this article, we shall learn What is "CauseValidation property", how to skip validation on clicking of a button and How to validate two different sets of Form fields separately?

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 not cause form validations because its CauseValidation property is set to false.

OUTPUT

Now we are going to see how to validate two different sets of Form fields separately. There are two possible cases i.e. (i)more than one html form on the page and they need to be validated  separately (ii)there are two sets of form elements and and they need to be validated separately and for achieving these kinds of validation, we can follow this approach.

ASPX PAGE

<fieldset>

<legend><b>User Login</b></legend>

<p>

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

ValidationGroup="LoginForm1" />

<asp:RequiredFieldValidator ID="req1" runat="server"

ErrorMessage="Mandatory !" ForeColor="Red" ControlToValidate="txtUserName"

ValidationGroup="LoginForm1" />

</p>

<p>

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

ValidationGroup="LoginForm1" />

<asp:RequiredFieldValidator ID="req2" runat="server"

ErrorMessage="Mandatory !" ForeColor="Red" ControlToValidate="txtPassword"

ValidationGroup="LoginForm1" />

</p>

<asp:Button ID="btnSubmit" runat="server" Text="Login"

OnClick="SubmitData1" ValidationGroup="LoginForm1" />

</fieldset>

<fieldset>

<legend><b>Admin Login</b></legend>

<p>

Username: <asp:TextBox ID="txtAUserName" runat="server"

ValidationGroup="LoginForm2" />

<asp:RequiredFieldValidator ID="req3" runat="server"

ErrorMessage="Mandatory !" ForeColor="Red" ControlToValidate="txtAUserName"

ValidationGroup="LoginForm2" />

</p>

<p>

Password: <asp:TextBox ID="txtAPassword" runat="server" TextMode="Password" ValidationGroup="LoginForm2" />

<asp:RequiredFieldValidator ID="req4" runat="server"

ErrorMessage="Mandatory !" ForeColor="Red" ControlToValidate="txtAPassword"

ValidationGroup="LoginForm2" />

</p>

<asp:Button ID="btnASubmit" runat="server" Text="Login"

OnClick="SubmitData2" ValidationGroup="LoginForm2" />

</fieldset>

CODE BEHIND

protected void SubmitData1(object sender, EventArgs e)

{

 

}

protected void SubmitData2(object sender, EventArgs e)

{

 

}

In the above code snippet, on .aspx page we have two sets of Login form (User Login and Admin Login) separately. To validate both forms separately we can distinguish their controls using ValidationGroup property. Notice that both forms have “LoginForm1” and “LoginForm2” as validation group to their controls that causes the forms to be validated separately.

OUTPUT

Hope this article was useful. Thanks for reading.

Keep reading my forth coming articles. To read my series of articles on ASP.NET,click here.

Page copy protected against web site content infringement by Copyscape

About the Author

SheoNarayan
Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com
http://www.snarayan.com
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)