1

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>
1
  • What exactly isn't working server side, are you looking at Page.IsValid and expecting it to be true? Commented Apr 21, 2015 at 8:53

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! My demo code doesn't quite mimic my real-world project where I need to know if a page is valid in Page_Load() but setting the correct Page.Validate("<groupName>") has fixed it for me. Really helpful - thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.