为什么我的 ASP.NET CustomValidator 无法验证?
我一定做错了什么。 我似乎无法执行 CustomValidator 的 ServerValidate 方法。
我有一个带有 CustomValidator 的 Visual Basic ASP.NET 页面...
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Friendly message goes here."
Display="Dynamic" />
<asp:Button ID="Button1" runat="server"
Text="Submit"
CausesValidation="True" />
对于此测试,我将验证设置为始终失败...
Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate args.IsValid = False End Sub
但是,单击按钮时,CustomValidator1_ServerValidate() 方法永远不会执行!
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Page.Validate() If Page.IsValid Then 'it executes the code here! End If End Sub
即使我明确验证该控制也不会......
CustomValidator1.Validate() 'does nothing?
我做错了什么?
I must be doing something wrong. I can't seem to execute my CustomValidator's ServerValidate method.
I've got a Visual Basic ASP.NET page with a CustomValidator...
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1"
ErrorMessage="Friendly message goes here."
Display="Dynamic" />
<asp:Button ID="Button1" runat="server"
Text="Submit"
CausesValidation="True" />
For this test, I've got the validation set to always fail...
Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate args.IsValid = False End Sub
But, when the button is clicked, the CustomValidator1_ServerValidate() method never executes!
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Page.Validate() If Page.IsValid Then 'it executes the code here! End If End Sub
Not even if I explicitly validate that control...
CustomValidator1.Validate() 'does nothing?
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
添加属性:
Add the property:
您是否将验证器控件提交按钮放在同一个验证组中?
Are you putting the validator control submit button in the same validation group?
首先,您似乎在上面的标记中缺少 OnServerValidate 属性。
其次,我将检查以确保 CustomValidator1_ServerValidate 已设置为 Textbox1 的 ServerValidate 事件的事件处理程序。 我曾经遇到过这样的情况:我更改了标记和代码隐藏中的验证方法的名称,但 IDE 没有自动更新传递给事件处理程序委托的订阅方法名称
Firstly, You seem to be missing the OnServerValidate attribute in your markup above.
Secondly, I would check to ensure that CustomValidator1_ServerValidate has been set up as an eventhandler for the ServerValidate event for Textbox1. I have had occasions where I have changed the name of the validate method in the markup and code-behind, but the IDE has not auto updated the subscribing method name passed to the eventhandler delegate
我知道这是一个愚蠢的问题(或者听起来像是一个愚蠢的问题!)。 但是您是否实际输入或更改了文本框中的值? 我认为如果文本框的内容没有改变,验证器就不会触发。
I know it's a daft question (or might sound like it!). But have you actually entered or changed the value in the textbox? I think the validator won't trigger without the contents of the textbox changing.