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.
Related
In ASP.NET a RequiredFieldValidator still validates a textbox control even if I disabled it on the client-side. I wanted to get rid of this behavior by using a CustomValidator. In the validation function I check if the text box is enabled. If that is the case I set the validator to valid.
function ValidateTextBox(sender, args) {
var $textbox = $('#txtFoo')
args.isValid = $textbox.prop('disabled') || $textbox.val();
}
This works fine on the client-side. The problem is, that I changed the state of the text box on the client-side using JavaScript. The server therefore does not know about the changed state (that the textbox control is disabled now). So when the ServerValidate event of the custom validator is raised (by calling Page.Validate for example) I cannot check whether the text box is disabled or not. Because the server does not know about this the Enabled property is always set to true, no matter what I do on the client-side.
How can I disable validation for controls when they are disabled or tell the server the state of the text box during Postback?
Thanks for your time.
Instead of making 'disabled' try to set textbox to 'ReadOnly' mode.
You can Enable or Disable your RequiredFieldValidator at the time of you are disabling the textbox using ValidatorEnable JavaScript method
(reference)
In our project we use ASP.net server-side controls (not my idea).
We have some client-side code that might disable some of those controls.
However, I'm unable to retrieve the SelectedValue of listboxes which have been disabled:
If ListboxA is disabled on the client side, then on postback, ListboxA.SelectedValue is empty (even though there is a selected value). if the listbox is not disabled- the selected value is returned correctly.
I found a workaround for this by simply enabling all the controls before postback, but that's... kinda stupid. anybody got a better idea?
P.S: example scenario: if a user chooses 'administrator' in the 'user type' listbox, the 'permissions' listbox has the value 'all' set, and becomes disabled, so that administrator can only be created with all permissions.
When the control is disabled, the client will not post the value back to the server, so you cannot retrieve the value that way. This would be better than the client sending data for disabled controls when we don't need it, so the workaround is not "stupid".
However, you have two (probably more) workarounds:
Re-enable the control just before Postback.
Save the value in a hidden field.
This is a strange one that has been bugging me today. Due to the fact that asp:RadioButtons get rendered in a span I decided to use a std input control with runat="server".
<input ID="rbDayRate" type="radio" runat="server" disabled="disabled" />
Now as you can see by default this control is disabled. It is enabled by jQuery if the user clicks on a checkbox. However once the state of the control gets to the serverside and I come to use it is not the correct value.
I have checked for all code that is modifying it after the postback data has been loaded and there is none. I have even checked the http post data in Fiddler2 and it is correctly being sent back by the browser.
What's even stranger is if I remove the disabled flag the control works fine.
Any ideas what's going on?!?!
Richard,
Reason is, you have disabled the control from the server side, even if you enabled it from client using JQuery, SERVER DOES NOT KNOW ABOUT IT. Therefore it keeps the same value as it is not expecting to change the value of a disabled control...
Try to disable it onload from client, should work this way....
You can try adding enableviewstate="true" property to the control. That should allow the control to keep its data/values after 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?
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.
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!