Spencer Prahl

.Net Developer & Architect

  Home  ::  Contact  ::  Syndication  ::  Login
  9 Posts  ::  3 Stories  ::  8 Comments  ::  5 Trackbacks  
Enhancing the ASP.Net Validation Summary Control

Enhancing the ASP.Net Validation Summary Control

The standard Validation Summary control in ASP.Net provides some useful functionality that enhances ASP.Net validation. However, there are instances when the standard functionality is not quite enough. For example, a client-side validator won’t suffice when input data must be validated against data in a database. Scott Juranek has provided a good way to add validation messages to a ValidationSummary control when server-side validation takes place. His project can be found on CodeProject. His project explains an instance where client-side validation is not practical when validating data from a database.

Scott’s example covers another scenario that I recently encountered. Say you have a DataGrid that has multiple editable columns and rows and each cell has its own validator. The validators each have the same values for the ErrorMessage and Text properties. When there are multiple failures in the DataGrid, the ValidationSummary control will display a message for each failed validator which could result in multiple messages listed, each with the same message. The result is a long list of repeated error messages in the ValidationSummary control (see below).

Figure 1 - Validation Summary with Multiple Errors

One way to get around this is to remove the value from the ErrorMessage property of each of the RequiredFieldValidator controls in the DataGrid. The result is that the ValidationSummary control no longer displays the errors that have occurred in the grid. This may be acceptable as long as the validation messages are displayed within the grid and the user can see them. But, what happens if the grid is large and some of the columns are scrolled off the page, out of site?

Figure 2 - Validation Summar with No Messages

The user can see some of the errors within the grid, but not all (a third column in Figure 2 contains errors that are not visible without scrolling the browser window). The answer is to use the enhanced ValidationSummary control. The custom ValidationSummary control allows server-side addition of custom error messages to the summary. The result looks something like the following.

Figure 3 - Validation Summary with Custom Message

Note the custom message in the summary. The code that produces the message simply does some sort of validation on the server and produces the custom message based on the number of errors that occurred.

1private void btnSubmit_Click(object sender, System.EventArgs e)
2{
3 try
4 {
5 Page.Validate();
6 // Do some data validation with the DataGrid data here
7 // If the validation fails, throw an exception
8 throw new ApplicationException();
9 }
10 catch
11 {
12 // Catch the exception and add a custom error message to the CustomSummary control
13 CustomSummary.AddErrorMessage("15 Required fields are missing data");
14 }
15}

In order to force validator controls to run at the server rather than on the client, set the EnableClientScript property to False. This disables the client-side validation that occurs for validators such as the RequiredFieldValidators in the DataGrid. The only drawback to this approach is that the custom message can only be entered server-side.

 

Thanks again to Scott Juranek for this. It proved to be very useful.

 


Feedback

# Enhancing the ASP.Net Validation Summary Control

# Use the .Net CodeDOM to compile C# On the Fly

# re: Enhancing the ASP.Net Validation Summary Control
Hello, what I would like to do is to add links to the validation summary control so that the users can lind directly to the invalid control. Do you know how to do this?

# re: Enhancing the ASP.Net Validation Summary Control
Thanks for the kind comments. Just was searching and found this.


Post Feedback
Title: 
Name: 
Url: 

Comments: 
Enter the code you see: