Summarizing Validation Messages
As you may have noticed, all the validation messages take up a lot of space
on the page梱ou may not want to sacrifice all that space for your
validation messages. ASP.NET provides its ValidationSummary control so that
you can display all the validation messages in one place as well as display
either nothing at all or a simple indicator next to each invalid control.
The sample page, shown in Figure 8.3, displays an asterisk next to each
invalid control and full error message text in the summary.
Figure 8.3. The ValidationSummary control lists all the ErrorMessage
properties for invalid controls in one place.
Adding the ValidationSummary control is simple: All you have to do is place
the control on your page. It automatically seeks out all the various
validation controls on the page, gathering the ErrorMessage property from
each control as necessary.
To set the text to be displayed within the validation controls themselves,
set the Text property for each control. This property overrides the
ErrorMessage property, and each control uses the Text property to determine
what to show within its own display. (If you don't specify the Text
property, as has been the case throughout this chapter, the controls
display their ErrorMessage property.) To add the ValidationSummary control
to your page, follow these steps:
Select the first validation control on the page and then Shift+click each
of the remaining validation controls, selecting them all.
In the Properties window, set the Text property (for all the selected
controls) to *.
Click directly above the Save button to place the insertion point within
the page. In the Toolbox window, double-click the ValidationSummary control
to insert an instance of this control.
Browse the page and then click Save without entering any data. Try entering
invalid data in some of the controls. When you submit the page, you should
see asterisks next to the invalid controls and a summary of the errors at
the bottom of the page.
TIP
If you want to modify the layout and behavior of the ValidationSummary
control, check out the DisplayMode property, which can be set to List,
BulletList, or Paragraph. Try them all to see how they affect the display.
In addition, you might check out the ShowMessageBox property, which allows
you to have up-level browsers (such as Internet Explorer) display a message
box alert containing the validation information.