[英]CustomValidator not working (asp.net vb)
I am using the CustomValidator for the first time but it doesn't seem to be firing DateExpireRequired_ServerValidate and just runs the code in the Click action. 我是第一次使用CustomValidator,但它似乎并未触发DateExpireRequired_ServerValidate,而只是在Click操作中运行了代码。
Been bugging me for a couple hours now! 现在烦了我几个小时! can anyone see a problem with what im doing?
谁能看到我在做什么问题?
The DropDownList in my code below is populated using Roles.GetAllRoles() 我下面的代码中的DropDownList使用Roles.GetAllRoles()填充
ASP.NET 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>
CODE BEHIND 后面的代码
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
Thanks J. 谢谢J.
You have Enabled="false"
in your custom validator definition. 您的自定义验证程序定义中具有
Enabled="false"
。 I assume this is disabling that validator. 我假设这正在禁用该验证器。
The answer was as in Menno van den Heuvel's comment above: 答案与Menno van den Heuvel的上述评论相同:
Do you have validateemptytext set to True? 您是否将validateemptytext设置为True? I see that you check for an empty string in your event handler, but the event handler won't be run if the bound control's value is empty.
我看到您在事件处理程序中检查了一个空字符串,但是如果绑定控件的值为空,则该事件处理程序将不会运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.