验证摘要不在适当的位置显示错误

时间:2015-09-02 22:51:56

标签: c# asp.net validation validationsummary

我的登录页面中有两个组logincreateUser。我有两个验证摘要loginSumcreateSum。我对每个控件都进行了必需的字段验证。如果我单击LoginCreate按钮而没有包含值的必填字段,则错误将显示在字段下方,而不是摘要中。

登录代码:

<div id="login" class="settings">
asp:Label ID="lblInvalid" runat="server" Visible="false" Display="Static">
        YOUR EMAIL OR PASSWORD WERE NOT FOUND. PLEASE REVIEW YOUR INFORMATION AND TRY AGAIN.<br /></asp:Label>
    <asp:Label ID="Label1" runat="server" Text="Email:" Width="100px" />
    <asp:TextBox ID="user" runat="server" /><br />
    <%-- validation for user login email --%>
    <asp:RegularExpressionValidator ID="validEmail"
        ControlToValidate="user"
        ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
        Display="Static"
        ValidationGroup="login"
        ErrorMessage="Email"
        SetFocusOnError="true"
        runat="server" 
        Text="*" /><br />
    <asp:Label ID="Label2" runat="server" Text="Password:" Width="100px" />
    <asp:TextBox ID="password" TextMode="password" runat="server" /><br />
    <%-- validation for user login password --%>
    <asp:RequiredFieldValidator ID="passReq"
        runat="server"
        ControlToValidate="password"
        ValidationGroup="login"
        ErrorMessage="Password"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Button ID="submit" runat="server" Text="Login" OnClick="login_Click"
        ValidationGroup="login" CausesValidation="true" /><br />
    <asp:ValidationSummary ID="loginSum"
        DisplayMode="BulletList"
        EnableClientScript="true"
        ShowSummary="true"
        HeaderText="You must enter a valid value for the following fields:"
        runat="server" />
</div>

创建代码:

<div id="create_account" class="settings">
    <asp:Label ID="Label10" runat="server" Text="Name:" Width="100px" />
    <asp:TextBox ID="txtName" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="nameReq"
        runat="server"
        ControlToValidate="txtName"
        ValidationGroup="createUser"
        ErrorMessage="You must put in a name!"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="Label3" runat="server" Text="Email Address:" Width="100px" />
    <asp:TextBox ID="txtNewUser" runat="server" /><br />
    <%-- validation for new email --%>
    <asp:RequiredFieldValidator ID="newUserReq"
        runat="server"
        ControlToValidate="txtNewUser"
        ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
        ValidationGroup="createUser"
        ErrorMessage="Email address"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="Label4" runat="server" Text="Password:" Width="100px" />
    <asp:TextBox ID="txtNewPass" TextMode="password" runat="server" /><br />
    <%-- validation for new password --%>
    <asp:RequiredFieldValidator ID="newPassReq"
        runat="server"
        ControlToValidate="txtNewPass"
        ValidationGroup="createUser"
        ErrorMessage="Password"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="Label5" runat="server" Text="Confirm Password:" Width="100px" />
    <asp:TextBox ID="txtConfirmPass" TextMode="password" runat="server" /><br />
    <%-- validation for confirm password --%>
    <asp:RequiredFieldValidator ID="confirmPassReq"
        runat="server"
        ControlToValidate="txtConfirmPass"
        ValidationGroup="createUser"
        ErrorMessage="Confirm password"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:CompareValidator ID="ComparePass" runat="server"
        ControlToValidate="txtConfirmPass"
        CssClass="ValidationError"
        ControlToCompare="txtNewPass"
        ErrorMessage="The passwords you entered must match" /><br />
    <asp:Label ID="label" Text="Address:" runat="server" Width="100px" />
    <asp:TextBox ID="txtAddress" TextMode="multiline" Columns="22" Rows="3" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="addressReq"
        runat="server"
        ControlToValidate="txtAddress"
        ValidationGroup="createUser"
        ErrorMessage="Address"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label6" Text="City:" runat="server" Width="100px" />
    <asp:TextBox ID="txtCity" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="cityReq"
        runat="server"
        ControlToValidate="txtCity"
        ValidationGroup="createUser"
        ErrorMessage="City"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label7" Text="State:" runat="server" Width="100px" />
    <asp:TextBox ID="txtState" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="stateReq"
        runat="server"
        ControlToValidate="txtState"
        ValidationGroup="createUser"
        ErrorMessage="State (abbreviation)"
        ValidationExpression="[a-zA-Z]{2}"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label8" Text="Zip Code:" runat="server" Width="100px" />
    <asp:TextBox ID="txtZip" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="zipReq"
        runat="server"
        ControlToValidate="txtZip"
        ValidationGroup="createUser"
        ErrorMessage="Zip code"
        ValidationExpression="[a-zA-Z]{9}"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Label ID="label9" Text="Phone Number:" runat="server" Width="100px" />
    <asp:TextBox ID="txtPhone" runat="server" /><br />
    <%-- validation for new user --%>
    <asp:RequiredFieldValidator ID="phoneReq"
        runat="server"
        ControlToValidate="txtPhone"
        ValidationGroup="createUser"
        ErrorMessage="Phone number"
        ValidationExpression="[a-zA-Z]{15}"
        SetFocusOnError="true" Display="Static" 
        Text="*" /><br />
    <asp:Button ID="create" runat="server"
        Text="Create Account" OnClick="create_Click"
        ValidationGroup="createUser" CausesValidation="true" /><br />
    <br />
    <asp:ValidationSummary ID="createSum"
        DisplayMode="BulletList"
        ShowSummary="true"
        EnableClientScript="true"
        HeaderText="You must enter a valid value for the following fields:"
        runat="server" />
</div>

任何人都可以帮助我或指导我一个有效的教程吗?我现在得到的只是所有内容的“*”,如果我拿走Text="*",那么它只会在每个相关控件下方显示ErrorMessage="_____",而不是像它应该的那样在验证摘要中显示。

1 个答案:

答案 0 :(得分:0)

您需要将验证组属性添加到所有验证器和两个摘要以及按钮(每个按钮只验证一个组),即

ValidationGroup="logingroup"
ValidationGroup="creategroup"

请参阅(^