The Validation Summary Control

Sometimes, you might want to simply summarize the errors on a page, perhaps because the errors might involve multiple fields, and so signaling an error on a single field might be misleading. For example, if we were to use a CompareValidator control to compare two new password entries, placing an error message next to one or the other of the fields could be misleading. On the other hand, it could just be a part of the user interface standards at your organization that you display a single error message, either at the top or the bottom of the page. How do you do that with ASP.NET? ASP.NET offers another kind of validation control, ValidationSummary. This control handles all the error messages from all validators and displays them in a single place. Listing 5-6 demonstrates how this control is used in ValidatorTestSummary.aspx. Listing 5-6 ValidatorTestSummary.aspx, showing how the ValidationSummary control is used is used

<script language="C#" runat=server>

void Validate_Click(Object sender, EventArgs E) {

Msg.Text="Page Valid";

void CustomServerVal (object source, ServerValidateEventArgs args) {

Msg.Text="ServerValidation called and TRUE returned."; args.IsValid=true;

Msg.Text="ServerValidation called and FALSE returned."; args.IsValid=false;

catch {

Msg.Text="ServerValidation called and FALSE returned."; args.IsValid=false;

<body> <form runat=server> <center> <h3>

<font face="Verdana" color=blue>

Validator Test Page - Summary

<input id="Range" type="text" runat=server size=10 /> </td> <td>

<ASP:RangeValidator ID="ValRange" ControlToValidate="Range" Display="None" Type="Integer" MinimumValue="1" MaximumValue="12" ErrorMessage="Range" runat=server /> <ASP:RequiredFieldValidator ControlToValidate="Range" Display="None" ErrorMessage="Range" runat=server />

Regular Expression Validation (nnn-nn-nnnn): </td>

<input id="RegEx" type="text" runat=server size=11 /> </td> <td>

<ASP:RegularExpressionValidator ID="ValRegEx" ControlToValidate="RegEx" runat="SERVER" Display="None"

ErrorMessage="Regular Expression" ValidationExpression= "[0-9]{3}-[0-9]{2}-[0-9]{4}" /> <ASP:RequiredFieldValidator ControlToValidate="RegEx" Display="None"

ErrorMessage="Regular Expression" runat=server />

Custom Validation (It wants you to enter "Hello" WITHOUT THE QUOTES): </td> <td>

<input type="text" id="txtCustom"

<ASP:CustomValidator ID="ValCustom" runat="server"

ControlToValidate="txtCustom" OnServerValidate="CustomServerVal"

Display="None" >

Custom </ASP:CustomValidator> <ASP:RequiredFieldValidator ControlToValidate="txtCustom" Display="None" ErrorMessage="Custom" runat=server />

<td colspan=3 align="center"> <asp:button text="Validate" OnClick="Validate_Click" runat=server>

<asp:Label id="Msg" ForeColor="red" Font-Name="Verdana"

Font-Size="10" runat=server /> <asp:ValidationSummary id="valSum"

DisplayMode="BulletList" ShowSummary="true" runat="server" HeaderText=

"You must enter a value in the following fields:"


</td> </tr> </table> </center> </form> </body> </html>

Figure 5-10 shows the page that is returned when all fields are left empty and the Validate button is clicked.

Figure 5-10 shows the page that is returned when all fields are left empty and the Validate button is clicked.

Figure 5-10 : The validation summary displayed when ValidatorTestSummary.aspx is submitted with no values entered The most important change in Listing 5-6 is the addition of a ValidationSummary control at the bottom of the page, below the button and label, as follows:

"VOu injit Kite*

<asp:ValidationSummary id="valSum"




HeaderText="You must enter a value in the following fields:"


Let's quickly review the attributes of the ValidationSummary control that aren't obvious by their names. DisplayMode is the attribute that tells ASP.NET how to display errors. The values allowed are part of the ValidationSummaryDisplayMode enumeration in the System.Web.UI.WebControls namespace. The allowed values are shown here:

■ BulletList Displays a bulleted list of the error messages

■ List Displays a list of the error messages

■ SingleParagraph Displays all error messages in a single paragraph The ShowSummary attribute accepts a true or false value and indicates whether the validation summary is shown in line within the HTML. Not shown in this example is the ShowMessageBox attribute, which controls whether the validation summary is displayed in a message box on the client. ShowMessageBox also expects a true or false value. The HeaderText attribute sets the text used as the header of the validation summary. Other changes made to ValidatorTestSummary.aspx (shown in Listing 5-6) from ValidatorTestRequired.aspx (shown in Listing 5-5) are the settings for the Display attribute (all set to None) and the error text, set to a user-friendly version of the control being validated so that the message makes sense in combination with HeaderText.

Was this article helpful?

0 0

Post a comment