Validate() function doesn't fire

别来无恙 提交于 2020-01-07 09:54:08

问题


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?


回答1:


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();
    }



回答2:


try this

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

            return isValid;
        }



回答3:


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



来源:https://stackoverflow.com/questions/22130096/validate-function-doesnt-fire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!