为什么我的ASP.NET的CustomValidator验证?
-
20-08-2019 - |
题
我一定是做错了什么。我似乎无法执行我的CustomValidator的方法的ServerValidate
我有一个Visual Basic ASP.NET页面用的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" />
对于这个测试,我已经得到了验证设置为总是失败......
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?
我在做什么错了?
解决方案
添加属性:
ValidateEmptyText="True"
其他提示
您把验证控件相同的验证组中提交按钮?
首先,你似乎缺少在上面你的标记的OnServerValidate属性。
其次,我会检查,以确保CustomValidator1_ServerValidate已被设置为用于所述Textbox1的事件的ServerValidate一个EventHandler。我曾场合我已经在标记改变validate方法的名称和代码隐藏,但IDE没有自动更新传递给EventHandler委托订阅方法名
我知道这是一个愚蠢的问题(或可能听起来像它!)。但你有没有实际输入或更改文本框中的值?我的认为的验证不会没有改变文本框的内容触发。
不隶属于 StackOverflow