views:

197

answers:

2

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?

A: 

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.

spark
A: 

I know the first gut never replied back, but I had the same issue and the code above did the trick. Thanks Spark.

Frank