Server side validation affecting client side validation

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
globrite
7/3/2008 9:45:50 PM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 12 Replies
👁️‍🗨️ 2011 Views

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
DotNetAdvisor
7/4/2008 12:29:29 AM

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
globrite
7/4/2008 9:52:00 AM

Can you post both your javascript and your server validation? 


Rob Mills

www.dotnetadvisor.com
0
DotNetAdvisor
7/4/2008 11:41:35 AM

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 upon

var 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 controls

for (i = 1; i

{

disableControl(enableControlsOnSelection.arguments[i]);

}

break;

}

}

}

}

}

function disableControl(control) {

// Try to disable complex list control type

disableListControl(control, true);

clearListControl(control);

 

// Try to disable simple control type

control.disabled = true;

control.value='';

}

function enableControl(control) {

// Try to enable complex control type

disableListControl(control, false);

 

// Try to enable simple control type

control.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 parameters

String 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
globrite
7/4/2008 1:35:51 PM

Oops!

String jScript = "enableControlsOnYesSelection(" + radioButtonID;

Should be

String jScript = "enableControlsOnSelection(" + radioButtonID;

1
globrite
7/4/2008 1:53:51 PM

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
DotNetAdvisor
7/5/2008 3:56:16 PM

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
globrite
7/7/2008 9:12:53 AM

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
DotNetAdvisor
7/7/2008 11:23:56 PM

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 (Thread closed)

Related Posts: