views:

1533

answers:

3

I have used ValidatorEnable to disable a RequiredFieldValidator in javascript. However on postback the control being validated by the validator is still validated ... even though I disabled the validator on the client.

I understand why this is happening since I've only disabled the client validation ... however is there a nice way to determine that I've disabled the client validation and to then disable the server validation on postback?

+1  A: 

It's not clear from your question, what you are disabling. The RequiredFieldValidator has an both an Enabled and an EnableClientScript property.

If you want to disable validation on both client and server you should set Enabled to false.

To disable just client side, set EnableClientScript to false.

HectorMac
+2  A: 

You could, when you disable the client validation (presumably with JavasScript?), also set up values in a hidden input that you could query in page load method. This way you can examine the value of the hidden input (via the Request.Form{] array) to disable the validators on the server side prior to the validation event firing.

Another option would be to override the pages Validate() method to disable the validators based on the same rules that hid them on the client side.

palehorse
+1  A: 

Peanut, I just encounted the same issue you are. On the client-side I have javascript that disables required field validators depending on their selections in the UI. However, the server side validation was still firing.

During load, I added a method to disable my validators using the same rules I have on the javascript. In my case, it depended on the user's selection of a radio button:

this.requiredValidator.Enabled = this.myRadioButton.Checked;

This seems to work on the SECOND page load, but not the first. After debugging, I discovered that the wrong radio button is considered to be checked when this line was firing. The View State/Post Data wasn't applied to the control at the point when I was checking it to disable my validator. The reason I placed this during load was to make sure I disabled the validator before it fired.

So, the solution seems to be to have something like the line above AFTER ViewState, but BEFORE validators.

Overloading the Validate() method, as palehorse suggested, worked and allowed me to ensure the server side validators were enabled/disabled based on the current selections of the user.

Jay S
Perfect - just the job - thanks !
Peanut