0

我第一次使用 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。

4

2 回答 2

1

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

于 2011-10-04T15:51:06.343 回答
0

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

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

于 2011-10-06T10:36:44.420 回答