ASP.NET - how to stop unrequired server validation - asp.net

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?

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.

Related

Is checking Page.IsValid required for server-side validation in asp.net webforms

I have several asp validation controls on my page. I have an asp.net button on my page that saves everything on the page.
In the OnClick server-side event of the button should I always be wrapping it in a (if Page.IsValid) statement?
It seems like the validation still works regardless? Or is that only the client-side validation that is working?
By default, validation will occur, fired by your button after Page_Load event.
Also, Page.IsValid make sense after calling Page.Validate(). Be aware that the latter occurs automatically in several scenarios.
Your question can be answered in more depth, but I won't go into more details as many others have already covered this subject. You can continue reading in this other SO question.
Page.Validate is called automatically however in some instances you are best calling the method in your own code. Calling the Page.Validate() method to trigger the server side validation.
You will then need to check the Page.IsValid property to find out if there are any validation errors or not.
It is then up to you to decide if you want to page to continue processing or if you want to just return the page.
If you do not check the property and change the flow the event handling code will be executed.
You can find out if server side validation is happening yourself by disabling JavaScript in your browser and then submitting the form. This will by-pass the client side validation.

Internal works of ASP.NET Validation controls in client and server side

I want to know how ASP.NET validation controls are working (eg: requiredfield validator, regular expression validator). What will really happening when clicking on the button? What are the events are fireing on client side? What really happening internally?
When you click on the button (if you you defined CausesValidation="true") button send request to server with __doPostBackWithOptions function aims which contains valiadtion group as parameter (if you defined ValidationGroup property for your button). __doPostBackWithOptions contains function Page_ClientValidate. In this case will be checked all validators which belong for this group. If result (Page_IsValid) will be true postback occures. If You don't define validationGroup (but CausesValidation true) will be checked all validators which is present on page. If defined CAusesValidation false will use _doPostBack function without validators checking.
More information here: http://msdn.microsoft.com/en-us/library/aa338815(v=vs.71).aspx and
http://msdn.microsoft.com/en-us/library/aa479045.aspx

Creating custom ASP.NET validator

I have created a custom control and a custom validator (extending BaseValidator). On custom control I have set ValidationProperty("Values"). The problem is that validation doesn't work when postback is sent unless I execute Page.Validate(). And when I call Page.Validate() all validators are executed which is not what I would expect on postback.
How do I create custom validator which would be executed when control value changes and validates just that control?
That is not how validators work. Unless you are using a ValidationGroup setting, all the validators on your page will automatically fire. You do NOT have to explicitly call Page.Validate(). You DO need to wrap your code in a check like this, however:
if(Page.IsValid)
{
//do something here
}
Unlike client-side validators, the server-side validation does NOT prevent the page from posting back and processing events as normal.
To create a control which only validates when the control value changes would require a bit of hackery, since the change event fires after the validators have been executed.
Have you tried using validation groups?

Disabling an ASP validation on an event click before it posts back and performs the validation

Can you disable an ASP validation on an event click before it posts back and performs the validation? This is for ASP with C#.
I would like to load some details into a bunch of text-boxes with field validators on them. However I need to disable the validation of these text-boxes in order to actually fill them, as the validation seems to occur at post-back, before code execution.
If you're wondering why I have field validators on when I'm loading in the data, it's because the fields hold user entered data too.
Buttons have a bool 'Cause Validation' setting.
Turn off validation and call it manually when you are ready.
What you want is to set the "EnableClientScript" set to false, then when your initial load is finished, switch that back on. It will load the client side validation scripts.
You could do the same thing with the "Enabled" switch as well.
So basically, if you want a button to load your forms and then turn on validation, you could just create a method that enables/disables all of your validation controls, and call that to enable them after you have successfully populated your forms.
Cheers!

Validators: Do they Stay disabled on postback when disabled ClientSide?

I have been learning alot about the standard asp.net Validators, and my latest discovery was about how to disable a validator client side, which was pretty cool.
now, if my initial post has the validator enabled, but client side, i disable it, does the server side recognize the client side change, and keep it, or does it get re-enabled the the page is sent back to the user?
Thanks!
Nate
.NET Server side validator controls will be reset to whatever they have been set to last in the server side code during a postback.
So for example if you have set a required field validator to rqvControl.enabled = true in its .aspx tag, then after a postback it will be enabled no matter what its state was client side.
If you are setting the state of a validator on the client side, and you want to persist it, then you will need to set a value that you can read in your server code during a postback. This can be as simple as setting a hidden field value from your javascript that is doing the enable/disable operation. In your codebehind just handle the enabled state of your validator based off the value in your hidden field.
How do you disable them on the client side? I would imagine that unless the act of disabling them on the client does a postback that the viewstate will remain unchanged and hence they will be reenabled on the next page refresh (just theorizing here).
Try it yourself and if it doesn't work, you could perhaps add a hidden field which you also set on the client when you disable the validator and then check that field on the server side during a postback to know whether to enable/disable them there.

Resources