Add your own error message to a ValidationSummary

There are time where you need to display a message to the users.  Here’s how to do it with a Validation Summary on your ASP.NET page.
I use this when I want to display an error message that doesn’t come from an exception or another validation.
This is helpful when you want all messages to have the same look and feel.
You MUST have a validation summary on your page otherwise nothing happens.

Below is the code.

CustomValidator val = new CustomValidator();
val.IsValid = false;
val.ErrorMessage = "My error message for the ValidationSummary";
this.Page.Validators.Add(val);

You create create a custom validator, set it to invalid, define the error message, and add it to your page.

[tweetmeme only_single=”false”]
Advertisements

Got something to add?

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