服务器端 customvalidator (asp + vb.net) 未触发

发布于 2024-11-07 21:31:05 字数 1103 浏览 1 评论 0原文

我知道这个问题已经被问了很多,但我有一个服务器端自定义验证器没有被解雇,我找不到原因(另外,我复制了从其他地方工作的示例,但无法使其工作...... )

代码:

<asp:CustomValidator ID="MyValidator" runat="server" ErrorMessage="My error message" OnServerValidate="MyValidator_OnServerValidate" />

验证函数

    Protected Sub MyValidator_OnServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles MyValidator.ServerValidate
    Dim i As Integer
    i = 0
    args.IsValid = False
End Sub

这是我正在做的最后一个测试(从其他问题复制),但我也尝试将验证器附加到一个控件(并添加 ValidateEmptyText)并删除 handles MyValidator.ServerValidate 并使用 onServerValidate 将其添加到标记中。

(我通常是一名 Java 程序员,所以这可能是我应该看到的一个非常基本的错误,不要低估我的 .Net 无知)

提前致谢

编辑:添加提交按钮的代码...

<asp:Button ID="lbGuardar" runat="server" CssClass="button" Text="Guardar" CausesValidation="true" ></asp:Button>

编辑2:在我的按钮代码上添加了Page.validate(),并且它不会调用验证器的服务器端。此外,正在调用自定义验证器(至少是客户端/javascript 端,但不是服务器端)

I know this has been asked a lot, but I have a server-side custom validator that is not being fired, and I cannot find why (also, I've copied examples that work from other places and cannot make it work...)

The code:

<asp:CustomValidator ID="MyValidator" runat="server" ErrorMessage="My error message" OnServerValidate="MyValidator_OnServerValidate" />

Validate function

    Protected Sub MyValidator_OnServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles MyValidator.ServerValidate
    Dim i As Integer
    i = 0
    args.IsValid = False
End Sub

This is the last test I'm doing (copied from other question), but I've also tried attaching the validator to one control (and adding the ValidateEmptyText) and removing the handles MyValidator.ServerValidate and adding it to the tag with onServerValidate.

(I'm usually a Java programmer, so it can be a very basic error I should have seen, don't underestimate my .Net ignorance)

Thanks in advance

EDIT: Add the code for the submit button...

<asp:Button ID="lbGuardar" runat="server" CssClass="button" Text="Guardar" CausesValidation="true" ></asp:Button>

EDIT 2: Added a Page.validate() on my button code, and it does not call the server side of the validator. Also, the custom validator is being called (at least the client/javascript side of it, but not the server side)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

七颜 2024-11-14 21:31:05

我突然想到,我会尝试将 Protected Sub 更改为 Public Sub。

编辑: 我刚刚测试了您的代码以及您所拥有的对我有用的代码。我进入“调试”并单击一个按钮,该按钮按其应有的方式发回到首先进入 MyValidator.ServerValidate 的位置。它将页面变为无效并显示错误消息。

Off the top of my head, I would try changing Protected Sub to Public Sub.

EDIT: I just tested your code and what you have works for me. I went into Debug and clicked a button that posted back where it went into MyValidator.ServerValidate first, as it should. It turns the page invalid and shows the Error Message.

冰雪之触 2024-11-14 21:31:05

应该是公共方法并且不需要句柄。

Public Sub MyValidator_OnServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

    Dim i As Integer
    i = 0
    args.IsValid = False

End Sub

Should be a public method and doesn't need the handles.

Public Sub MyValidator_OnServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)

    Dim i As Integer
    i = 0
    args.IsValid = False

End Sub
爱你不解释 2024-11-14 21:31:05

没能成功。删除了自定义验证器,并在逻辑内部进行验证。

Did not manage to make it work. Removed custom validator, and made validations inside the logic.

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