-1

Here is my button.

<asp:Button ID="btnNext" runat="server" Text="Next" Style="display: none" OnClick="btnNext_Click" CausesValidation="true" ValidationGroup="vgLinR"/>

When I write ValidationGroup="vgLinR" in aspx side validation works. But I have 2 different validation group. So I need fire these 2 validation group in one button.

so I write that code at code behind :

protected void btnNext_Click(object sender, EventArgs e)
{
       Page.Validate("vgLinR");
       Page.Validate("vgLogR");
}

but it doesn't work. Why? How can I do that?

neverwinter
  • 810
  • 2
  • 15
  • 42
  • why are you using two validation group , if you want to validate those two validation from on button . use only one validation group instead of twp – Amit Kumar Mar 02 '14 at 16:27
  • I have 2 radio button (radiobtn1 and radiobtn2) in one group and 4 text box which names are a, b, c, d. And end of the page there is standard button. I add asp:requiredFieldValidators to all of textboxes. So when I clicked my button one validationsummary works for all of textboxes. I want to do that if radiobtn1 was clicked, validationsummary1 should work for textbox a and b, if radiobtn2 was clicked, validationsummary2 should work for should textbox c and d. If radiobtn1 was clicked I dont need to control c and d.they can be null. But radiobtn2 was clicked they can't be null. – neverwinter Mar 02 '14 at 16:30
  • for radio button validation , i have already provided you solution , for button see my answer . hope it will help you. – Amit Kumar Mar 02 '14 at 16:32

3 Answers3

0

it will work for you ..

  <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
        <br />
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox3" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
        <br />
        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox4" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="validate all" OnClick="Button1_Click"/>  <br />

    </div>
    </form>

and write code for onclick event

protected void Button1_Click(object sender, EventArgs e)
    {
        Page.Validate();
    }
Amit Kumar
  • 5,888
  • 11
  • 47
  • 85
0

try this

 public bool Validate() 
        {
            var isValid = false;
Page.Validate("vgLinR");
            isValid = Page.IsValid;
            if (isValid) 
            {
            Page.Validate("vgLogR");
                isValid = Page.IsValid;
            }

            return isValid;
        }
santosh singh
  • 27,666
  • 26
  • 83
  • 129
-1

Saw your answer reply for Amit a bit too late. I have updated my answer accordingly. May be you can use a similar idea if it does not fit your requirement.

In my code I am using a single ValidationSummary control without any validation group specified. Also remove the validation group from your button. Textbox a and b can be in one validation group, vg1 whereas Textbox c and d can be in another, vg2. I am not sure how you have set up your validation groups.

protected void btnNext_Click(object sender, EventArgs e)
{
    if (RadioButton1.Checked)
    {
        Page.Validate("vg1");
        ValidationSummary1.ValidationGroup = "vg1";

    }
    else if (RadioButton2.Checked)
    {
        Page.Validate("vg2");
        ValidationSummary1.ValidationGroup = "vg2";
    }


    if (Page.IsValid)
    {
        //do something in here
    }
}

The above code will do a server side validation. To do it on the client side as well, you would need to add a bit of javascript.

Look at another post to enable/disable Validation Group from JQuery or Javascript

Community
  • 1
  • 1
TechnoBrat
  • 277
  • 1
  • 7
  • My real problem is that Page.Validate("vgLinR"); doesn't do anything – neverwinter Mar 02 '14 at 16:47
  • No it wont, the code 'Page.Validate("vgLinR")' will not do anything as the line after it, 'Page.Validate("vgLogR")' is overriding it. I will update my answer as soon as I get it working. – TechnoBrat Mar 02 '14 at 16:57
  • vgLinR is provided as a validation group to textbox, a & b whereas vgLogR is provided as a validation group for textbox, b & c? – TechnoBrat Mar 02 '14 at 17:08