【问题标题】:CustomValidator not working (asp.net vb)CustomValidator 不工作 (asp.net vb)
【发布时间】:2011-10-04 15:44:27
【问题描述】:

我是第一次使用 CustomValidator,但它似乎没有触发 DateExpireRequired_ServerValidate,只是在 Click 操作中运行代码。

已经烦了我几个小时了!任何人都可以看到我在做什么的问题吗?

下面代码中的 DropDownList 是使用 Roles.GetAllRoles() 填充的

ASP.NET

<asp:Label ID="lUserRole" runat="server" AssociatedControlID="tUserRole">User Role:</asp:Label>
<asp:DropDownList ID="tUserRole" runat="server" CausesValidation="True">
</asp:DropDownList>

<asp:Label ID="lDateExpire" runat="server" AssociatedControlID="tDateExpire">Date Expire:</asp:Label>
<asp:TextBox ID="tDateExpire" runat="server"></asp:TextBox>
<asp:CustomValidator ID="DateExpireRequired" runat="server"
                ControlToValidate="tDateExpire" ErrorMessage="Date Expire is required for 'Users'." OnServerValidate="DateExpireRequired_ServerValidate"
                ToolTip="Date Expire is required for 'Users'." CssClass="frmError"></asp:CustomValidator>

代码隐藏

    Sub DateExpireRequired_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)

    If tUserRole.SelectedValue = "User" Then
        If tDateExpire.Text <> "" Then
            args.IsValid = False
        Else
            args.IsValid = True
        End If
    Else
        args.IsValid = True
    End If

End Sub

谢谢J。

【问题讨论】:

  • 您是否将 validateemptytext 设置为 True?我看到您在事件处理程序中检查了一个空字符串,但是如果绑定控件的值为空,则事件处理程序将不会运行。
  • 就是这样,谢谢...不能接受您的回答,尽管它是评论。再次感谢。
  • @MennovandenHeuvel 请发布您的答案,以便可以投票并设置为答案。

标签: asp.net vb.net customvalidator custom-validators


【解决方案1】:

您的自定义验证器定义中有Enabled="false"。我认为这是禁用该验证器。

【讨论】:

  • 是的,不启用自定义验证器将禁用它。
  • 对不起,我在尝试不同的方式时添加了它,但只是将其取出并再次尝试以确保它仍然无法正常工作。
【解决方案2】:

答案就像上面 Menno van den Heuvel 的评论:

您是否将 validateemptytext 设置为 True?我看到您在事件处理程序中检查了一个空字符串,但如果绑定控件的值为空,则事件处理程序将不会运行。

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多