从 Javascript 更新 ValidatorCallOut 错误消息

发布于 2024-08-02 01:28:51 字数 993 浏览 1 评论 0原文

我正在尝试通过 javascript 更新使用 ValidatorCallOut 的 CustomValidator 的错误消息。 基本上它检查输入的数字是否是指定数字的增量。 我有一些代码会在第一次运行时更新错误消息,但之后它将不再更新错误消息,尽管通过 javascript 警报我看到这些值实际上正在更新。 这是我正在使用的客户端 JavaScript 验证函数:

    function checkIncrement(sender, args) {
    var incrementValue = parseInt(sender.orderIncrement); // Custom attribute registered with RegisterExpandoAttribute
    var remainder = args.Value % incrementValue;

    if ((remainder) != 0) {

        var remainder, lowRange, highRange;
        lowRange = parseInt(args.Value - remainder);
        highRange = parseInt(lowRange + incrementValue);

        sender.errormessage = "Closest possible values are <b>" + lowRange + "</b> or <b>" + highRange + "</b>"; // Gets updated once, but not after that
        alert("Low Range: " + lowRange); // always updated with current value

        args.IsValid = false;
        return;
    }

    args.IsValid = true;
}

关于如何在每次运行验证时更新错误消息,有什么想法吗?

I am trying to update the error message for a CustomValidator that uses a ValidatorCallOut via javascript. Basically its checking to see if a number entered is an increment of a specified number. I have some code that will update the error message the first time it is run, but after that it will no longer update the error message, although through javascript alerts I see the values are actually being updated. Here is the client side javascript validation function I'm using:

    function checkIncrement(sender, args) {
    var incrementValue = parseInt(sender.orderIncrement); // Custom attribute registered with RegisterExpandoAttribute
    var remainder = args.Value % incrementValue;

    if ((remainder) != 0) {

        var remainder, lowRange, highRange;
        lowRange = parseInt(args.Value - remainder);
        highRange = parseInt(lowRange + incrementValue);

        sender.errormessage = "Closest possible values are <b>" + lowRange + "</b> or <b>" + highRange + "</b>"; // Gets updated once, but not after that
        alert("Low Range: " + lowRange); // always updated with current value

        args.IsValid = false;
        return;
    }

    args.IsValid = true;
}

Any idea on how to keep the error message updated every time this is run to validate?

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

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

发布评论

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

评论(1

捂风挽笑 2024-08-09 01:28:51

请尝试以下操作:

sender.errormessage = "Your message here";
var cell = sender.ValidatorCalloutBehavior._errorMessageCell;
// cell is going to be null first time.
if (cell != null) {
    cell.innerHTML = "Your message here";
}

消息在初始化后一直保留的原因是因为最初并未创建 Callout。 创建后,标注将被隐藏,并且不会在后续显示时重新创建。 因此,它在创建时初始化的消息将保留并持续存在。 上面的代码是一种 hack,确实应该有一种类似于 set_ErrorMessage 的方法,但没有。

Try the following:

sender.errormessage = "Your message here";
var cell = sender.ValidatorCalloutBehavior._errorMessageCell;
// cell is going to be null first time.
if (cell != null) {
    cell.innerHTML = "Your message here";
}

The reason why the message sticks once it has been initialized is because Callout is not created initially. Once it has been created, Callout is just hidden and is not recreated on subsequent showing. Thus, message that it was initialized with when it was created will stick and persist. The above code is a hack and there really should be a method along the lines of set_ErrorMessage, but there isn't one.

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