CustomValidator 不工作 (asp.net vb)
我是第一次使用 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。
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.
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()
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的自定义验证器定义中有
Enabled="false"
。我认为这会禁用该验证器。You have
Enabled="false"
in your custom validator definition. I assume this is disabling that validator.答案如上面 Menno van den Heuvel 的评论所示:
您是否将 validateemptytext 设置为 True?我看到您在事件处理程序中检查空字符串,但如果绑定控件的值为空,则事件处理程序将不会运行。
The answer was as in Menno van den Heuvel's comment above:
Do you have validateemptytext set to 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.