Hi there
I have some client side validation that is working fine and enables/disables buttons based on user selections. This works great. However, when I perform server side validation using custom validators (like checking if the user has entered < HTML > tags or decimals in the integer boxes) and the server side validation returns isValid = false then all of the JavaScript validation that I've already done when the user clicked each control is lost. I don't understand why the enabled/disabled state of the control is not maintained. What is calling the JavaScript refresh?
Thanks
![]() |
0 |
![]() |
Are you saying that the client-side validation is not preventing a postback? If so then there's an error in your custom validator's javascript. Any error that occurs during validation usually prevents the any further processing which is what also prevents the execution of the script which stops the postback.
Rob Mills
www.dotnetadvisor.com
![]() |
0 |
![]() |
Thanks for replying.
No, the client side validation is working in that I don't see a complete refresh of the page and post back. What I see are the validation messages appearing if a mistake is present. However, other JavaScript that I have on the page is also affected. I have some client side JavaScript that opens and closes radiobuttonlists based on user selections - for example, if they select that they had no previous illnesses then the buttons on type and condition and free text comments grey out and become disabled. However, upon server side validation using custom validators and not even necessarily on these particular controls, after the validation happens the buttons/textareas etc I have disabled with JavaScript re-open. The page is not refreshing or posting back so why would the buttons open up?
Thanks
![]() |
0 |
![]() |
Can you post both your javascript and your server validation?
Rob Mills
www.dotnetadvisor.com
![]() |
0 |
![]() |
Ok, it is quite complicated; I hope you are able to understand how it all happens. However, there are many of these validators on my ASP page so I've only shown a sample.
The JavaScript to enable/disable
function enableControlsOnSelection(){
var numberOfControls = enableControlsOnSelection.arguments.length; // This should always be a RadioControlList used to base our selection criteria uponvar tableBody = enableControlsOnSelection.arguments[0].childNodes[0]; for (var i=0;i{
for (var j=0;j{
var currentTd = tableBody.childNodes[i].childNodes[j];var listControl = currentTd.childNodes[0];if (listControl.checked)
{
if (listControl.value == "Yes" || listControl.value == "Maybe"){
// User has selected the "Yes" radio button // Enable controls // Note: We start at 1, as the 0 element is our radio control list that // we have used above to determine whether the user has selected Yes or No for (i = 1; i{
enableControl(enableControlsOnSelection.arguments[i]);
}
break;}
else if (listControl.value == "NO" || listControl.value == "Not Yet"){
// User has selected the "No" radio buttons // Disable and clear controlsfor (i = 1; i{
disableControl(enableControlsOnSelection.arguments[i]);
}
break;}
}
}
}
}
function disableControl(control) { // Try to disable complex list control typedisableListControl(control, true);clearListControl(control);
// Try to disable simple control typecontrol.disabled = true;
control.value=
'';}
function enableControl(control) { // Try to enable complex control typedisableListControl(control, false);// Try to enable simple control typecontrol.disabled = false;
}
The ASP that adds the JavaScript to the control:
public static void enableControlsOnSelection(RadioButtonList rb, WebControl control){
String radioButtonID = "document.getElementById('" + rb.ClientID + "')"; String jScript = "enableControlsOnYesSelection(" + radioButtonID; // Extract the ID for the control you wish to enable and append it to the javascript argument list // The javascript function will handle the variable number of parametersString controlID = "document.getElementById('" + control.ClientID + "')";jScript +=
"," + controlID;jScript += ");";rb.Attributes.Add(
"onclick", jScript);}
The code that calls the method on the ASP page:
ClientSideValidation.enableControlsOnSelection(rblA, rblB);
A sample of the server side validation:
protected void TextboxValidator_ServerValidate(object source, ServerValidateEventArgs args){
if (Comments.Text.Contains("|")){
args.IsValid = false;
}
else if (Comments.Text.Contains(System.Environment.NewLine)){
args.IsValid = false;}
else if (Comments.Text.Contains("<") || CommentsCOL.Text.Contains(">")){
args.IsValid = false;}
}
![]() |
0 |
![]() |
Oops!
String jScript = "enableControlsOnYesSelection(" + radioButtonID;Should be
String jScript = "enableControlsOnSelection(" + radioButtonID;
![]() |
1 |
![]() |
Maybe I'm not totally understanding what you're trying to accomplish but it seems to me that your validator could be replaced with a regularexpressionvalidator. If the problem had to do with your custom validator then that would solve it. Have you tried using that?
Rob Mills
www.dotnetadvisor.com
![]() |
0 |
![]() |
Hi there
The custom validator I showed is the simplest one I had - there are many others that do quite complex things like validate whether or not some fields are required based on user selection. Although a range validator might work for one, it would not work for them all.
In any case, both would return isValid = false if the validation was not met, and I think I would be in the same position with either server validator. Either would cause the page to return with a validation error and re-open my other JavaScript events on the page.
Just to refresh what I'm doing, I have two methods to validate user input - JavaScript to open/close web controls based on user selections (and therefore the JavaScript events are tied into the webcontrols) and also server validation to check things like the required fields (based on user selections) etc. The JavaScript works fine and the server side works fine independently, however, when the server side validation is triggered (isValid = false) then the page appears with the server validation messages, as expected, but the other events from my own JavaScripts need to be re-fired again. I need to re-click these buttons again to trigger the buttons' onclick events.
I don't understand how this can happen without a full post back. I have a JavaScript countdown timer on the page that resets on the window.onload event and during server side validation this counter is not reset - therefore I know that the Window's onload event is not called.
Any ideas?
![]() |
0 |
![]() |
I would separate the javascript that is used to disable controls from the javascript that is used for validation. Because any postback will then require the javascript to run again when the page loads in the browser. Then you'll need to come up with some kind of way to maintain the state using javascript. Probably the best way is using hidden fields.
Now are some of your validators only validating on the server? In other words did you not provide client validation for them?
Rob Mills
www.dotnetadvisor.com
![]() |
0 |
![]() |
Some thoughts:
1. If you set the disabled attribute on the client-side to true, the browser does NOT send a value back to the server for that control. (Its an Http behavior, not ASP.NET).
2. The state of the disabled attribute is never transferred to the server. Only the value attribute of each ,
and
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
![]() |
0 |
![]() |
I'd be interested to find out if anyone has a solution to this.
I have a similar problem. My page has some textboxes, radio buttons, etc that are enabled and disabled by javascript depending on what the user clicks on.
For example on a page that allows users to change their contact details (residential, postal, email, phone):
By default when the page loads, the fields for the user's address are disabled (by javascript invoked onload).
If they click on the Change Address checkbox, the javascript then sets the fields enabled and they can enter in their new details.
However if they just click on the submit button without changing anything, or if there's something wrong with the data entered, then the validation (RequiredFieldValidator/CompareValidator/etc) posts an error message. All of the fields on the page are reset to be enabled when the page refreshes. The OnLoad function does not appear to be invoked.
![]() |
0 |
![]() |
daviessi:I'd be interested to find out if anyone has a solution to this.
I have a similar problem. My page has some textboxes, radio buttons, etc that are enabled and disabled by javascript depending on what the user clicks on.
For example on a page that allows users to change their contact details (residential, postal, email, phone):
By default when the page loads, the fields for the user's address are disabled (by javascript invoked onload).
If they click on the Change Address checkbox, the javascript then sets the fields enabled and they can enter in their new details.
However if they just click on the submit button without changing anything, or if there's something wrong with the data entered, then the validation (RequiredFieldValidator/CompareValidator/etc) posts an error message. All of the fields on the page are reset to be enabled when the page refreshes. The OnLoad function does not appear to be invoked.
Take a look at this article I wrote on my blog about performing conditional validation:
http://www.dotnetadvisor.com/Blog/ConditionalValidation.aspx
Rob Mills
www.dotnetadvisor.com
![]() |
0 |
![]() |
Hi Rob,
Your article is close to what I am looking for, but not quite. The problem is not with the validation as such. The validation is working correctly and catching all the things I want it to.
The problem is what happens when the validation returns an error. The page appears to refresh, but the javascript function that should be called as part of the OnLoad() is not being called. This javascript checks the control checkboxes and sets fields disabled/enabled as required.
When the page refreshes with the validation error message, half the fields on the page are enabled when they shouldn't be.
![]() |
0 |
![]() |