Pakk
Pakk

Reputation: 1339

Enable/Disable aspx Validator via js not holding true in code behind?

I'd like to know what I'm missing regarding disabling these via js only, and not having to add repeat code in the code behind.

I'm using this code to disable / enable Validators:

validator: {
        enable: function (id) {
            console.log('enable',id);
            var validator = document.getElementById(id);
            ValidatorEnable(validator, true);
        },
        disable: function (id) {
            console.log('disable',id);
            var validator = document.getElementById(id);
            ValidatorEnable(validator, false);
        }
    },

It works great, until i make a postback to save some data, ... where these disabled validators are not disabled.

So i have to run some repeated code within the OnLoad() if IsPostBack is true to disable the validators I've disabled via js already.

Edit: Removed any code as it wasn't required to achieve an answer. Answer: Server side must also disable the elements, can't be done explicitly for these reasons: found here, thanks to @ConnersFan: https://msdn.microsoft.com/en-us/library/aa479045.aspx

Upvotes: 1

Views: 1121

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73731

As you can see in ASP.NET Validation in Depth, the prefered method to enable/disable a validator on the client side is to use ValidatorEnable:

var validator = document.getElementById(id);
ValidatorEnable(validator, false);

On the server side, you must also enable/disable the validator explicitely (I don't think you can avoid that):

validator.Enabled = false;

This change will be preserved in client code, unlike the change in Javascript code which is not preserved on postback.

Upvotes: 1

Related Questions