服务器端 customvalidator (asp + vb.net) 未触发
我知道这个问题已经被问了很多,但我有一个服务器端自定义验证器没有被解雇,我找不到原因(另外,我复制了从其他地方工作的示例,但无法使其工作...... )
代码:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我突然想到,我会尝试将 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.
应该是公共方法并且不需要句柄。
Should be a public method and doesn't need the handles.
没能成功。删除了自定义验证器,并在逻辑内部进行验证。
Did not manage to make it work. Removed custom validator, and made validations inside the logic.