Summarizing Errors: The ValidationSummary ControlImagine that you have a form with 50 form fields. If you use only the Validation controls discussed in the previous sections of this chapter to display errors, seeing an error message on the page might be difficult. For example, you might have to scroll down to the 48th form field to find the error message. Fortunately, Microsoft includes a ValidationSummary control with the Validation controls. You can use this control to summarize all the errors at the top of a page, or wherever else you want. All the properties and methods of this control are listed in Table 3.5. Table 3.5. ValidationSummary Properties, Methods, and Events
The page in Listing 3.17 illustrates how you can use the ValidationSummary control to display a summary of errors (see Figure 3.3). Listing 3.17 ValidationSummary.aspx<Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs ) If IsValid Then Response.Redirect( "ThankYou.aspx" ) End If End Sub </Script> <html> <head><title>ValidationSummary.aspx</title></head> <body> <form Runat="Server"> <asp:ValidationSummary HeaderText="There are problems with the following form fields:" Runat="Server" /> <p> First Name: <br> <asp:TextBox ID="txtFirstname" Runat="Server" /> <asp:RequiredFieldValidator ID="reqVal1" ControlToValidate="txtFirstname" Text="You must enter a first name!" ErrorMessage="First Name" Runat="Server" /> <p> Last Name: <br> <asp:TextBox ID="txtLastname" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtLastname" Text="You must enter a last name!" ErrorMessage="Last Name" Runat="Server" /> <p> Occupation: <br> <asp:TextBox ID="txtOccupation" Runat="Server" /> <asp:RequiredFieldValidator Text="You must enter an occupation!" ControlToValidate="txtOccupation" ErrorMessage="Occupation" Runat="Server" /> <p> <asp:Button Text="Submit" OnClick="Button_Click" Runat="Server"/> </form> </body> </html> The C# version of this code can be found on the CD-ROM. Figure 3.3. Summarizing errors with the ValidationSummary control.
Notice how each Validation control is assigned an error message with the ErrorMessage property. These error messages are displayed in the ValidationSummary control whenever a problem occurs with a form field. Don't confuse the ErrorMessage and Text properties. Typically, you use the Text property of a Validation control to display an error message next to a form field, and you use the ErrorMessage property to display a message in the ValidationSummary control. By default, the ValidationSummary control displays error messages in a bulleted list. However, you also have the option of displaying the messages in a nonbulleted list or within a single paragraph. To control how the ValidationSummary control formats its summary of errors, modify its DisplayMode property. Figure 3.4 shows how the ValidationSummary control displays error messages with each of the different settings of the DisplayMode property. Figure 3.4. Different ValidationSummary display modes.
Displaying Pop-Up Error MessagesHave you ever seen those obnoxious pop-up error messages that some sites use to report validation errors? You can provide these messages for your users, too! You can enable the ValidationSummary control's ShowMessageBox property to display error messages in a dialog box. Listing 3.18 demonstrates how you can enable this property (see Figure 3.5 for the output). Figure 3.5. Enabling the ValidationSummary control's ShowMessageBox property.
NOTE If you want to show the error message summary only in the pop-up dialog box and not on the page itself, set the ValidationSummary control's ShowSummary property to False . Listing 3.18 ValidationSummaryPopUp.aspx<Script Runat="Server"> Sub Button_Click( s As Object, e As EventArgs ) If IsValid Then Response.Redirect( "ThankYou.aspx" ) End If End Sub </Script> <html> <head><title>ValidationSummaryPopUp.aspx</title></head> <body> <form Runat="Server"> <asp:ValidationSummary ShowMessageBox="True" HeaderText="There are problems with the following form fields:" Runat="Server" /> <p> First Name: <br> <asp:TextBox ID="txtFirstname" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtFirstname" Text="You must enter a first name!" ErrorMessage="First Name" Runat="Server" /> <p> Last Name: <br> <asp:TextBox ID="txtLastname" Runat="Server" /> <asp:RequiredFieldValidator ControlToValidate="txtLastname" Text="You must enter a last name!" ErrorMessage="Last Name" Runat="Server" /> <p> Occupation: <br> <asp:TextBox ID="txtOccupation" Runat="Server" /> <asp:RequiredFieldValidator Text="You must enter an occupation!" ControlToValidate="txtOccupation" ErrorMessage="Occupation" Runat="Server" /> <p> <asp:Button Text="Submit" OnClick="Button_Click" Runat="Server"/> </form> </body> </html> The C# version of this code can be found on the CD-ROM. |