最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ASP.NET - how to stop unrequired server validation - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Jan 6, 2009 at 16:00 PeanutPeanut 19.4k20 gold badges74 silver badges78 bronze badges 2
  • 1 If you're disabling it, why do you need a RequiredFieldValidator? – John Sheehan Commented Jan 6, 2009 at 16:01
  • 1 because I'm hiding elements of the page on the client - and these contain validators. I disable the validators client side when I hide these elements. Then when I postback I get the server validation issue since it doesn't know when I've disabled. – Peanut Commented Jan 6, 2009 at 16:05
Add a ment  | 

3 Answers 3

Reset to default 3

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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论