【问题标题】:CompareValidator inside an UpdatePanel - VS2008UpdatePanel 中的 CompareValidator - VS2008
【发布时间】:2008-11-14 19:47:42
【问题描述】:

我正在使用 UpdatePanel 并希望将 CompareValidator 放在两个文本框上,以验证用户输入的密码和确认是否相同。

这是开箱即用的(我有 VS2008 并且正在使用 .NET 3.5),但有一个小问题:

一旦用户点击第一个文本框,验证就会触发,在他们有机会输入第二个文本框之前。这不会导致任何真正的问题,以编程方式(所有发生的只是错误消息显示,当他们输入确认时它消失了)但我们的测试人员说这是一个问题。它不会通过 UA 测试,直到他们点击“保存”才触发验证。

如何让 CompareValidator 在两个框中都输入文本之前不触发?

编辑:

这是一个标记示例。

    <div>
        <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" /></div>    
    </div>
    <div>
        <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
    </div>
    <asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation" ControlToValidate="txtPassword" ControlToCompare="txtConfirmPassword" runat="server" ErrorMessage="Passwords do not match"></asp:CompareValidator>

以上内容包含在页面上 UpdatePanel 的 ContentTemplate 中的控件中。

(为简洁起见,删除了 CSS 类和样式)

【问题讨论】:

  • 请为验证者发布您的标签标记

标签: .net ajax validation updatepanel


【解决方案1】:

尝试切换它,以便在确认文本框而不是密码文本框上完成验证。这样,在您修改确认文本框或提交表单之前,它不会触发。而且您可能希望在密码文本框上有一个必填字段验证器。

<div>
    <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" />
         <asp:RequiredFieldValidator runat="server" ID="passwordRequiredValidator"
                                     ControlToValidate="txtPassword"
                                     ValidationGroup="PublishPassValidation"
                                     ErrorMessage="Password is required."  />    
    </div>    
</div>
<div>
    <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
</div>
<asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation"
                      ControlToValidate="txtConfirmPassword"
                      ControlToCompare="txPassword" runat="server"
                      ErrorMessage="Passwords do not match">
</asp:CompareValidator>

【讨论】:

  • RFV 已添加到两个框中,我听取了您的建议。坦率地说,这是次优的(因为行为仍然存在,只是不太可能被看到)但这是一件非常小的事情,应该满足 QA 测试人员的要求。
【解决方案2】:

您可以关闭该验证器的客户端验证。

EnableClientScript="false"

不过,这意味着要往返于服务器以报告无效状态,并且您必须确保在继续之前检查页面确实有效。

Page.Validate("PublishPassValidation");

if (Page.IsValid)
{
    // Do Stuff
}

【讨论】:

    【解决方案3】:

    我感觉您在更新面板上启用了孩子作为触发器?

    用户是否在密码框中按“ENTER”?您能否确认更新面板是否出于某种原因在移动焦点后执行了部分刷新?

    如果是这样,它将触发验证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2010-09-21
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多