自定义验证器不工作
我有一个 CustomValidator,用于检查文本框中输入的文本是否与数据库中的某些字段匹配。这一切以前都工作得很好,但从那以后我对我的页面进行了相当多的修改,它不再工作了。我不认为我改变了任何会影响这一点的事情,但显然我做了。我的所有其他验证器(必填字段验证器)都正常工作,但我的 CustomValidator 没有响应。
所以无论如何,这是我的代码:
CustomValidator:
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken."></asp:CustomValidator>
VB codebehind:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
'Checking for duplicate course numbers
'get values
Dim checkPrefix = txtCoursePrefix.Text
Dim checkNum = txtCourseNum.Text
'db connectivity
Dim myConn As New OleDbConnection
myConn.ConnectionString = AccessDataSource2.ConnectionString
myConn.Open()
'select records
Dim mySelect As New OleDbCommand("SELECT 1 FROM tableCourse WHERE prefix=? AND course_number=?", myConn)
mySelect.Parameters.AddWithValue("@checkPrefix", checkPrefix)
mySelect.Parameters.AddWithValue("@checkNum", checkNum)
'execute(Command)
Dim myValue = mySelect.ExecuteScalar()
'check if record exists
If myValue IsNot Nothing Then
CustomValidator1.SetFocusOnError = True
args.IsValid = False
End If
End Sub
一切正常,直到 CustomValidator1.SetFocusOnError = True 和 args.IsValid = False。我已经测试了 If 语句,它工作正常,它返回 true,并且我放入其中的其他任何内容都会执行。
I have a CustomValidator that checks if text entered in textboxes matches certain fields in a database. This was all working great before, but I have modified my page quite a bit since then and it is no longer working. I didn't think I changed anything that would affect this, but apparently I did. All my other validators (required field validators) are working correctly, but my CustomValidator isn't responding.
So anyway, here is my code:
CustomValidator:
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken."></asp:CustomValidator>
VB codebehind:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
'Checking for duplicate course numbers
'get values
Dim checkPrefix = txtCoursePrefix.Text
Dim checkNum = txtCourseNum.Text
'db connectivity
Dim myConn As New OleDbConnection
myConn.ConnectionString = AccessDataSource2.ConnectionString
myConn.Open()
'select records
Dim mySelect As New OleDbCommand("SELECT 1 FROM tableCourse WHERE prefix=? AND course_number=?", myConn)
mySelect.Parameters.AddWithValue("@checkPrefix", checkPrefix)
mySelect.Parameters.AddWithValue("@checkNum", checkNum)
'execute(Command)
Dim myValue = mySelect.ExecuteScalar()
'check if record exists
If myValue IsNot Nothing Then
CustomValidator1.SetFocusOnError = True
args.IsValid = False
End If
End Sub
Everything is working up until CustomValidator1.SetFocusOnError = True and args.IsValid = False. I have tested the If statement and it's working correctly, it returns true and anything else I put inside of it executes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用自定义验证器时应了解的事项:
如果您使用
ValidationGroup
进行验证,请不要忘记将其添加到您的CustomValidator
中。设置
ControlToValidate
属性。当
ControlToValidate
控件为空时,CustomValidator
控件永远不会触发,除非您设置ValidateEmptyText=true
。使用
ClientValidationFunction="customClientValidationFunction"
时,请使用以下签名:Things you should know when using customvalidators:
If you are validating using a
ValidationGroup
, don't forget to add it to yourCustomValidator
.Set the
ControlToValidate
property.A
CustomValidator
control never fires when theControlToValidate
control is empty unless you setValidateEmptyText=true
.When using
ClientValidationFunction="customClientValidationFunction"
use the following signature:您应该在
CustomValidator
上设置属性ValidateEmptyText="true"
。在这种情况下,将始终调用客户端和服务器函数。它为我解决了问题。
You should set the property
ValidateEmptyText="true"
on theCustomValidator
. The client and server functions will always be called in that case.It solved the problem for me.
如果处理程序被调用,并且您成功地将 args.IsValid 设置为 false,那么它的作用是将 Page.IsValid 设置为 false。但不幸的是,这并不能阻止表格的提交。您需要做的是检查处理表单提交的代码中的 Page.IsValid 属性,就像在提交按钮处理程序中一样。
因此,除了您发布的代码(听起来似乎工作正常)之外,请确保您的提交处理程序有类似的代码(C# 示例):
If the handler is getting called, and you're successfully setting the args.IsValid to false, then what that does is it sets Page.IsValid to false. But unfortunately, that doesn't stop the form from being sumbitted. What you need to do is check that Page.IsValid property in your code that handles your form submit, like in the submit button handler.
So in addition to the code you posted, which sounds like it is working correctly, make sure that you have something like this for your submit handler (C# example):
使用此
OnServerValidate="CustomValidator1_ServerValidate"
作为示例,它会起作用......
Gaurav Agrawal
Use this
OnServerValidate="CustomValidator1_ServerValidate"
like an example and it will work....
Gaurav Agrawal
首先,将验证组放在验证器和按钮上。如果这也不起作用,请输入 OnClientClick='CheckValidate();'并声明将调用 page_clientvalidate 方法以及参数的函数..验证组。这肯定会起作用。如果这不起作用,请将调试器放入 javascript 方法中并调试相同的方法
First of all, put validation group on validators and the button. If that too doesnt work, put OnClientClick='CheckValidate();' and declare the function which will call page_clientvalidate method along with the parameter.. validation group. This would surely work. If that is not working put debugger in the javascript method and debug the same