ASP.NET ValidationGroup无法在服务器端工作

时间:2015-04-20 17:44:12

标签: c# asp.net validation

我有一个包含两组控件的页面,每个集合的ValidationGroup设置不同,但如果我尝试正确填写一个表单,则另一个表单的验证器会停止触发Click事件。

我注意到它的行为通常是客户端,但按钮会导致回发事件,服务器验证程序似乎没有遵守验证组。我不确定我做错了什么。当然,内置的验证器都支持服务器端的验证组吗?

以下是一个完整的独立示例:

<%@ 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 个答案:

答案 0 :(得分:2)

删除显式的完整页面验证 - 即此行:  Page.Validate();它忽略了验证组。

您会注意到Page.Validate()也覆盖了允许您指定确切验证组的内容 - 例如Page.Validate("RIGHT")。这是出于框架目的; Web表单基础结构已经为您调用了正确的Validate(),因此无需显式调用它。