jump to navigation

CustomValidator and the ValidationSummary Control April 26, 2010

Posted by codinglifestyle in ASP.NET, jQuery, Uncategorized.
Tags: ,
trackback

ASP.NET validators can be tricky at times.  What they actually do isn’t particularly hard, but we have all had issues with them or quickly find their limits when they don’t meet our requirements.  The CustomValidator control is very useful for validating outside the constraints of the pre-defined validators: required fields, regular expressions, and the like which all boil down to canned javascript validation.  CustomValidators are brilliant as you can write your own client-side functions and work within the ASP.NET validation framework.  They are also unique in that they allow for server-side validation via an event.

However, there is a common pitfall when used in combination with the ValidationSummary control.  Normally, I would avoid using ShowMessageBox option as I believe pop-ups are evil.  However, where I work this is the norm and the problem is the CustomValidator’s error isn’t represented in the summary popup. 

When the ASP.NET validators don’t live up to our requirements we really must not be afraid to poke around Microsoft’s validation javascript.  It contains most of the answers to the questions you read about on the net (to do with ASP.NET validation… it isn’t the new Bible/42).  Quickly we identify the function responsible for showing the pop-up.  ValidationSummaryOnSubmit sounds good, but as the name implies it occurs only on submit.  However my validator failed after submit and now I need the popup to show what errors occurred.  I could see from the script window that this function could be called but programmatically registering the startup script wasn’t working.  So I used a jQuery trick to call the function after the DOM had loaded.

So drumroll please, there is the information you want to copy and paste in to your CustomValidator event:

if (!args.IsValid)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), “key”, “$(function() { ValidationSummaryOnSubmit(‘MyOptionalValidationGroup’)});”, true);
}

Now my server-side validation will bring up the ValidationSummary messagebox.

Advertisements

Comments»

1. k - April 5, 2011

Do you have to have jquery setup in your solution for this to work.

codinglifestyle - April 6, 2011

No, jquery isn’t required. This technique just leverages Microsoft’s validator javascript.

2. k - April 8, 2011

Thanks for answering my question. I get an error when I try the code above. Microsoft JScript runtime error: Object expected. There is no ValidationonSummaryOnSubmit function. Below I have included the html source. Thanks for any help in advance.

(function() {var fn = function() {$get(“ctl00_ctl00_ToolkitScriptManager1_HiddenField”).value = ”;Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();$(function() { ValidationSummaryOnSubmit(‘SummaryPop’)});
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == “function”) {
ValidatorOnLoad();
}

function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: