【问题标题】:Can you use an asp:customvalidator with an asp:ChangePassword control?您可以将 asp:customvalidator 与 asp:ChangePassword 控件一起使用吗?
【发布时间】:2009-05-28 21:19:42
【问题描述】:

我正在寻找有关如何将自定义验证控件用于 asp:ChangePassword 控件的想法。在 asp:CustomValidator 控件上设置“ControlToValidate”属性的简单尝试会导致:

“passwordValidation”的 ControlToValidate 属性引用的控件“changePassword1”无法验证。

谢谢。

【问题讨论】:

    标签: asp.net customvalidator


    【解决方案1】:

    导致此错误消息的根本问题是 ChangePassword 控件没有在类级别设置“ValidationProperty”属性。如前所述,您可以在 CustomValidator 中省略 ControlToValidate 属性。这可能是 ChangePassword 控件的最佳方案。

    如果您尝试将 CustomValidator 与自定义控件一起使用,请在自定义控件的类上设置 ValidationProperty:

    [DefaultProperty("StateName"), ValidationProperty("StateName")]
    public class StateBox: CompositeControl
    {
        ...
    

    如果要将 CustomValidator 与未设置 ValidationProperty 属性的库控件一起使用,请从库控件派生一个类并在派生类上设置 ValidationProperty。

    【讨论】:

      【解决方案2】:

      如果您使用 javascript 直接获取控件,则可以使用 CustomValidator 验证任何内容。无法设置 ControlToValidate 意味着您无法获取 EventArgs 中的数据,必须自己获取。

      【讨论】:

        【解决方案3】:

        您需要将 ChangePassword 控件更改为模板化控件才能使用自定义验证器。请务必将自定义验证器的验证组设置为与模板中的其他控件相同。或者,ChangePassword 控件具有可能满足您需求的 NewPasswordRegularExpression 属性。

        【讨论】:

          【解决方案4】:

          是的,只需使用模板:

          http://quickstarts.asp.net/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/ctrlref/login/ChangePasswordTemplates.src

          http://quickstarts.asp.net/quickstartv20/aspnet/doc/ctrlref/login/changepassword.aspx

          只要 textbox-id 是“用户名”(或您想要验证的任何内容),您就可以在模板中添加任何类型的验证器。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-06-15
            • 2023-03-24
            • 2020-11-23
            • 1970-01-01
            • 2014-01-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多