ValidationSummary Control

I l @ ve RuBoard

ValidationSummary Control

We saw in our second example (refer to Listing 8.2) that it is possible to use the Page.IsValid property to dynamically display validation control ErrorMessage properties. This last control is built to do just this, but without our having to write any code. Simply by placing this control on the page and setting a few properties, we will achieve all the functionality we had in that earlier example, and then some.

Unlike all the previous controls, the ValidationSummary control does not inherit from the BaseValidator class. Instead, it is derived from the System.Web.UI.WebControls.WebControl class. Its important properties are detailed in Table 8.8.

Table 8.8. ValidationSummary Class Properties
Method Type Description
DisplayMode ValidationSummaryDisplayMode Determines how the errors are listed when the summary is displayed. One of " BulletList ", " List ", or " SingleParagraph ".
HeaderText String Sets the header text to display the list of errors.
ShowMessageBox Boolean Determines whether or not a message box should be used to summarize the page's errors.
ShowSummary Boolean Determines whether or not a summary of all errors should be displayed.

The ValidationSummary control is the ideal way to summarize the validation error messages for an entire form. It includes several alternatives for layout and display, and can be placed anywhere within your form tags. To demonstrate how easy this control is to use, we will repeat the example from Listing 8.2 in Listing 8.7, but we will use the ValidationSummary control this time, instead of our own custom code.

Listing 8.7 Adding a ValidationSummary Control (Example0807.aspx).
 <%@ Page %> <HTML>     <HEAD>         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >     </HEAD>     <body>     <form method="post" runat="server">     <asp:ValidationSummary Runat="server" ID="valsummary"     DisplayMode="SingleParagraph"></asp:ValidationSummary>     <table>         <tr>             <td>                 Age:             </td>             <td>                 <asp:textbox runat="Server" columns="6" id="age"></asp:textbox>             </td>             <td>                 <asp:requiredfieldvalidator runat="Server" id="age_required"     controltovalidate="age" display="dynamic" errormessage="Age is required.">                 *                 </asp:requiredfieldvalidator>                 <asp:rangevalidator runat="Server" id="age_range"     controltovalidate="age" display="dynamic"     errormessage="Age must be an integer between 18 and 65." minimumvalue="18"     maximumvalue="65" Type="Integer">                 *                 </asp:rangevalidator>             </td>         </tr>     </table>     <asp:button id="save_button" runat="Server" text="Save"></asp:button>     </form>     </body> </HTML> 

Note that in this example it is no longer necessary to disable client-side scripting or force the ClientTarget to be for a DownLevel browser. The result is the same as in the earlier example when there is an error, as we see in Figure 8.4.

Figure 8.4. Example0807.aspx with some invalid data

I l @ ve RuBoard


Asp. Net. By Example
ASP.NET by Example
ISBN: 0789725622
EAN: 2147483647
Year: 2001
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net