Multiple ValidationSummary和RequiredFieldValidators

时间:2014-07-02 14:47:34

标签: asp.net

我的页面上有两个验证组,名为" login"和"注册"。每个组都有自己的ValidationSummary控件,还有许多RequiredFieldValidators。我遇到的问题是表单正在验证两个组而不仅仅是一个组。

这是代码(我省略了很多简化的HTML):

<div id="divRegisterBox">

  <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="register" />

  <asp:TextBox ID="tbRegisterEmail" runat="server" CssClass="form-control" placeholder="Required" ValidationGroup="register" />
  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="tbRegisterEmail" runat="server" Text="" ErrorMessage="Please enter your email address." ValidationGroup="register" />
  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" CssClass="errorMsg" ControlToValidate="tbRegisterEmail" runat="server" Text="" ErrorMessage="Please enter a valid email address." ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" ValidationGroup="register" />

  <asp:TextBox ID="tbRegisterPassword" name="result-password" runat="server" CssClass="form-control" placeholder="Required" TextMode="Password" ValidationGroup="register" />
  <asp:RequiredFieldValidator ID="RequiredFieldValidator2" CssClass="errorMsg" ControlToValidate="tbRegisterPassword" runat="server" Text="" ErrorMessage="Please enter your password." ValidationGroup="register" />

  <asp:Button ID="btnMobileRegister" runat="server" Text="Register" OnClick="btnMobileRegister_Click" ValidationGroup="register" />

</div>

<div id="divLoginBox">

  <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="login" />

  <asp:TextBox CssClass="form-control" name="result-email" ID="tbResultEmail" placeholder="Required" runat="server" ValidationGroup="login"></asp:TextBox>
  <asp:RequiredFieldValidator ID="RequiredFieldValidator3" ControlToValidate="tbResultEmail" runat="server" Text="" ErrorMessage="Please enter your email address." ValidationGroup="login" CssClass="errorMsg" />
  <asp:RegularExpressionValidator ID="RegularExpressionValidator2" CssClass="errorMsg" ControlToValidate="tbResultEmail" runat="server" Text="" ErrorMessage="Please enter a valid email address." ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" ValidationGroup="login" />

  <asp:TextBox CssClass="form-control" name="result-password" ID="tbResultPassword" placeholder="Required" TextMode="Password" runat="server" ValidationGroup="login"></asp:TextBox>
  <asp:RequiredFieldValidator ID="RequiredFieldValidator4" CssClass="errorMsg" ControlToValidate="tbResultPassword" runat="server" Text="" ErrorMessage="Please enter your password." ValidationGroup="login" />

  <asp:Button ID="btnLoginMobile" runat="server" CssClass="btn btn-primary btn-block" Text="Login" OnClientClick="return MobileLoginSubmit();" ValidationGroup="login" OnClick="btnLoginMobile_Click" />

</div>

当我单击任一Button控件时,ValidationSummary控件会在页面上显示所有验证控件的验证消息。就好像他们都是同一个验证组的一部分。

我尝试添加CausesValidation =&#34; true&#34;和&#34;假&#34;对按钮但它似乎没有效果。

2 个答案:

答案 0 :(得分:0)

在你的CodeBehind上,你可以做类似的事情......

Page.Validate("register");
if (Page.IsValid)
  {
      //Keep calm and carry on
  }

else
   {
       foreach (BaseValidator invalidField in validators)
            {  
              //Do something with the invalid fields 
            }
    }

这样,您只需验证您指定的组。

有道理吗?

答案 1 :(得分:0)

您的问题是Display="None"

divRegisterBox div中,您的验证人都有Display="None",但divLoginBox div中的验证人没有。{/ p>

Display="None"不会内嵌显示消息。单击登录按钮时所看到的并不是两个摘要都显示,而是以两种不同的方式显示相同的错误消息。