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;
}

关于如何在每次运行验证时更新错误消息的任何想法?

4

1 回答 1

3

尝试以下操作:

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.

于 2009-09-25T20:55:09.243 回答