简体   繁体   中英

ASP.NET ValidationGroup not working server-side

I have a page with two sets of controls, with ValidationGroup set differently for each set, but if I attempt to fill in one form correctly, the validators for the other form stop the Click event from firing.

I've noticed that it's behaving normally client-side, but the button then causes a postback event and the server validators do not seem to be honouring the validation groups. I'm unsure what I'm doing wrong. Surely the built in validators all support validation groups on the server side?

Below is a complete standalone example:

<%@ Page Language="C#" %>
<script runat="server">
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid) label.Text = "OK: " + ((Button)sender).ID.ToString();
    }
</script>
<!DOCTYPE html> 
<html>
    <head runat="server">
        <style>
            body { font-family: sans-serif;}
            .warning { color: red;}
        </style>
    </head>
    <body>
        <form id="f1" runat="server">

            <h1>Validator Test</h1>

            <asp:Label ID="label" runat="server" Font-Bold="true" ForeColor="Green" EnableViewState="false" /><br />


            <div style="width:40%; float:left; height:200px; border:1px solid #eee; padding:15px; margin:5px;">             
                <h2>Left Form:</h2>

                Email: <asp:TextBox ID="leftBox" runat="server" Columns="20" MaxLength="80" Width="160px" ValidationGroup="LEFT" />

                <asp:RequiredFieldValidator ID="left2" runat="server" ControlToValidate="leftBox" 
                    ErrorMessage="You must enter a value." ValidationGroup="LEFT">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="rev8" runat="server"  ControlToValidate="leftBox" ValidationGroup="LEFT" Text="*"
                        ErrorMessage="Email address format is invalid." ValidationExpression="^[\.\-\w]+@([\w\-]+\.)+\w+$" />
                <asp:Button ID="LeftSubmitButton" runat="server" Text="Save Left" OnClick="SubmitButton_Click" ValidationGroup="LEFT"  />
                <asp:ValidationSummary ID="LeftValidationSummary" runat="server" HeaderText="Error:" ValidationGroup="LEFT" CssClass="warning" />
            </div>

            <div style="width:40%; float:left; height:200px; border:1px solid #eee; padding:15px; margin:5px;"> 
                <h2>Right Form:</h2>

                Email: <asp:TextBox ID="rightBox" runat="server" Columns="20" MaxLength="80" Width="160px" ValidationGroup="RIGHT"  />
                <asp:RequiredFieldValidator ID="rfv9" runat="server" ControlToValidate="rightBox"
                    ErrorMessage="You must enter an value." ValidationGroup="RIGHT">*</asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="rev10" runat="server"  ControlToValidate="rightBox" ValidationGroup="RIGHT" Text="*"
                        ErrorMessage="Email address format is invalid." ValidationExpression="^[\.\-\w]+@([\w\-]+\.)+\w+$" />
                <asp:Button ID="RightSubmitButton" runat="server" Text="Save Right" OnClick="SubmitButton_Click" ValidationGroup="RIGHT"  />
                <asp:ValidationSummary ID="RightValidationSummary" runat="server" HeaderText="Error:" ValidationGroup="RIGHT" CssClass="warning" />
            </div>

        </form>
    </body>
</html>

Remove the explicit full page validation - that is this line: Page.Validate(); it disregards the validation groups.

You'll notice that Page.Validate() has also override that allows you to specify the exact validation group - like Page.Validate("RIGHT") . This is intended for framework purposes; web form infrastructure already calls the proper Validate() for you so no need to call it explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM