自定义验证器不工作

发布于 2024-11-10 13:15:21 字数 1541 浏览 7 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(5

久伴你 2024-11-17 13:15:21

使用自定义验证器时应了解的事项:

如果您使用 ValidationGroup 进行验证,请不要忘记将其添加到您的 CustomValidator 中。

设置 ControlToValidate 属性。

ControlToValidate 控件为空时,CustomValidator 控件永远不会触发,除非您设置 ValidateEmptyText=true

使用 ClientValidationFunction="customClientValidationFunction" 时,请使用以下签名:

function customClientValidationFunction(sender, arguments) {
   arguments.IsValid = true; //validation goes here
}

Things you should know when using customvalidators:

If you are validating using a ValidationGroup, don't forget to add it to your CustomValidator.

Set the ControlToValidate property.

A CustomValidator control never fires when the ControlToValidate control is empty unless you set ValidateEmptyText=true.

When using ClientValidationFunction="customClientValidationFunction" use the following signature:

function customClientValidationFunction(sender, arguments) {
   arguments.IsValid = true; //validation goes here
}
月野兔 2024-11-17 13:15:21

您应该在 CustomValidator 上设置属性 ValidateEmptyText="true"。在这种情况下,将始终调用客户端和服务器函数。

它为我解决了问题。

You should set the property ValidateEmptyText="true" on the CustomValidator. The client and server functions will always be called in that case.

It solved the problem for me.

无声静候 2024-11-17 13:15:21

如果处理程序被调用,并且您成功地将 args.IsValid 设置为 false,那么它的作用是将 Page.IsValid 设置为 false。但不幸的是,这并不能阻止表格的提交。您需要做的是检查处理表单提交的代码中的 Page.IsValid 属性,就像在提交按钮处理程序中一样。

因此,除了您发布的代码(听起来似乎工作正常)之外,请确保您的提交处理程序有类似的代码(C# 示例):

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
    {
        // by simply returning, the error message for the CustomValidator will be displayed
        return;
    }
    // do processing for valid form here
}

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):

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
    {
        // by simply returning, the error message for the CustomValidator will be displayed
        return;
    }
    // do processing for valid form here
}
我做我的改变 2024-11-17 13:15:21

使用此

OnServerValidate="CustomValidator1_ServerValidate"

作为示例,它会起作用......

<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken." OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

Gaurav Agrawal

Use this

OnServerValidate="CustomValidator1_ServerValidate"

like an example and it will work....

<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="txtCoursePrefix" ErrorMessage="Course number is already taken." OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>

Gaurav Agrawal

债姬 2024-11-17 13:15:21

首先,将验证组放在验证器和按钮上。如果这也不起作用,请输入 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

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