ASP.NET - Multiple Validation Group for a button

Asked By Mash B on 15-Apr-10 08:49 AM
I have 2 panels, each panel has few controls with one button control and has one validation group.

Panel1 button can trigger independently . But Panel2 button has to validate Panel1 validations and then its own validation and then trigger.
So is there any way where we can give multiple validation group for a button  ?
i want it to be done on client side .
Mohan Raj Aryal replied to Mash B on 15-Apr-10 08:56 AM
You can use ValidationGroup property of your controls with ASP.NET Validation control.
You can refer following examples:
http://www.dotnet-guide.com/validationgroups.html
http://www.codeproject.com/KB/aspnet/groupvalidator.aspx
Web Star replied to Mash B on 15-Apr-10 08:56 AM

u can put different group validation in different javascript function and than u will call that function on button click

btnSubmit.Attribute.Add("OnClick", "javascript:jsfunctioname();")

and put condition for call different group

Mash B replied to Mohan Raj Aryal on 15-Apr-10 09:07 AM
As i mentioned in my scenario, whn i will cleck on button2 it shud also validate button1 validation group.
Below is manipulated screenshot.
To clarify again button1 is having one validation group and button2 is having other validation group.
button1 can trigger independently of button2. but button2 should not trigger if button1 validation group dosent validate



Mohan Raj Aryal replied to Mash B on 15-Apr-10 12:28 PM

I don't think it is possible with Validation Group. You can do something to validate group 1 controls manually.
Use Javascript function for the validation group 1 text boxes. for that write javascript function something like this:

function validateGroup1()
        {
            if(document.getElementById("TextBox1").value !="" && document.getElementById("TextBox2").value !="")
             {
                //show invalidate group 1 message
                return false;
             }
             else
             {
                //Page is valid
                return true;
             }   
              
        }

and call this function on click of Button 1. To do this you can add attribute of button1 on page_load as: 

Button1.Attributes.Add("onClick", "return validateGroup1();");

For Button 2 you can still use validation group with the controls to validate.

I hope this helps.
mohd shareef replied to Mohan Raj Aryal on 16-Jun-11 01:02 AM
Hi,

You can programmaticall achieve this by calling Page.Validate(group) on the server before checking Page.IsValid. For example:

Partial Class _Default
  Inherits System.Web.UI.Page

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Page.Validate("Group1")
    Page.Validate("Group2")
    If (Page.IsValid) Then
      Response.Write("Valid")
    End If
  End Sub
End Class

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
      ErrorMessage="TextBox1" ValidationGroup="Group1">TextBox1</asp:RequiredFieldValidator><br />
    <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="Group1" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
      ErrorMessage="TextBox2" ValidationGroup="Group2">TextBox2</asp:RequiredFieldValidator>
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Group2" />
    <br />
    <asp:Button ID="Button2" runat="server" Text="Button" /></div>
  </form>
</body>
</html>


I Hope It May Help u...


Thanks
-m2d
mohd shareef replied to Mohan Raj Aryal on 16-Jun-11 01:36 AM
Hi,

You can programmaticall achieve this by calling Page.Validate(group) on the server before checking Page.IsValid. For example:

Partial Class _Default
  Inherits System.Web.UI.Page

  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Page.Validate("Group1")
    Page.Validate("Group2")
    If (Page.IsValid) Then
      Response.Write("Valid")
    End If
  End Sub
End Class

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>Untitled Page</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
      ErrorMessage="TextBox1" ValidationGroup="Group1">TextBox1</asp:RequiredFieldValidator><br />
    <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="Group1" />
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
      ErrorMessage="TextBox2" ValidationGroup="Group2">TextBox2</asp:RequiredFieldValidator>
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Group2" />
    <br />
    <asp:Button ID="Button2" runat="server" Text="Button" /></div>
  </form>
</body>
</html>


I Hope It May Help u...


Thanks
-m2d