为什么我的 ASP.NET CustomValidator 无法验证?

发布于 2024-07-12 08:48:42 字数 1092 浏览 3 评论 0原文

我一定做错了什么。 我似乎无法执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呢古 2024-07-19 08:48:42

添加属性:

 ValidateEmptyText="True"

Add the property:

 ValidateEmptyText="True"
分开我的手 2024-07-19 08:48:42

您是否将验证器控件提交按钮放在同一个验证组中?

Are you putting the validator control submit button in the same validation group?

小苏打饼 2024-07-19 08:48:42

首先,您似乎在上面的标记中缺少 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

枕梦 2024-07-19 08:48:42

我知道这是一个愚蠢的问题(或者听起来像是一个愚蠢的问题!)。 但是您是否实际输入或更改了文本框中的值? 我认为如果文本框的内容没有改变,验证器就不会触发。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文