Partilhar via


ValidationSummary Control (General Reference) 

Displays a list of all validation errors on the Web page.

<asp:ValidationSummary
    AccessKey="string"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CssClass="string"
    DisplayMode="List|BulletList|SingleParagraph"
    EnableClientScript="True|False"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    HeaderText="string"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    ShowMessageBox="True|False"
    ShowSummary="True|False"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

Remarks

The ValidationSummary control allows you to summarize the error messages from all validation controls on a Web page in a single location. The summary can be displayed as a list, a bulleted list, or a single paragraph, based on the value of the DisplayMode property. The error message displayed in the ValidationSummary control for each validation control on the page is specified by the ErrorMessage property of each validation control. If the ErrorMessage property of the validation control is not set, no error message is displayed in the ValidationSummary control for that validation control. You can also specify a custom title in the heading section of the ValidationSummary control by setting the HeaderText property.

You can control whether the ValidationSummary control is displayed or hidden by setting the ShowSummary property. The summary can also be displayed in a message box by setting the ShowMessageBox property to true.

For more information on the ValidationSummary control, see the System.Web.UI.WebControls.ValidationSummary class.

Example

The following code example demonstrates how to use the ValidationSummary control to summarize the input controls that fail validation on the page.

Security noteSecurity Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

 <h3>ValidationSummary Sample</h3>
<br />

<form id="Form1" runat="server">

   <table cellpadding="10">
      <tr>
         <td>
           <table style="background-color:#eeeeee; padding:10">

               <tr>
                  <td colspan="3">
                     <b>Credit Card Information</b>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     Card Type:
                  </td>
                  <td>
                     <asp:RadioButtonList id="RadioButtonList1" 
                          RepeatLayout="Flow"
                          runat="server">

                        <asp:ListItem>MasterCard</asp:ListItem>
                        <asp:ListItem>Visa</asp:ListItem>

                     </asp:RadioButtonList>
                  </td>
                  <td align="center" rowspan="1">
                     <asp:RequiredFieldValidator
                          id="RequiredFieldValidator1"
                          ControlToValidate="RadioButtonList1"
                          ErrorMessage="Card Type."
                          Display="Static"
                          InitialValue="" 
                          Width="100%" 
                          Text="*"
                          runat="server"/>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     Card Number:
                  </td>
                  <td>
                     <asp:TextBox id="TextBox1" 
                          runat="server" />
                  </td>
                  <td>
                     <asp:RequiredFieldValidator
                          id="RequiredFieldValidator2"
                          ControlToValidate="TextBox1" 
                          ErrorMessage="Card Number. "
                          Display="Static"
                          Width="100%"
                          Text="*" 
                          runat="server"/>
                  </td>
               </tr>

               <tr>
                  <td></td>
                  <td>
                     <asp:Button id="Button1" 
                          Text="Validate" 
                          runat="server" />
                  </td>
                  <td></td>
               </tr>
            </table>
         </td>
         <td valign="top">
            <table cellpadding="20">
               <tr>
                  <td>
                     <asp:ValidationSummary id="valSum" 
                          DisplayMode="BulletList"
                          EnableClientScript="true"
                          HeaderText="You must enter a value in the following fields:"
                          runat="server"/>
                  </td>
               </tr>
            </table>
         </td>
      </tr>
   </table>

</form>
 <h3>ValidationSummary Sample</h3>
<br />

<form id="Form1" runat="server">

   <table cellpadding="10">
      <tr>
         <td>
    <table style="background-color:#eeeeee; padding:10">

               <tr>
                  <td colspan="3">
                     <b>Credit Card Information</b>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     Card Type:
                  </td>
                  <td>
                     <asp:RadioButtonList id="RadioButtonList1" 
                          RepeatLayout="Flow"
                          runat="server">

                        <asp:ListItem>MasterCard</asp:ListItem>
                        <asp:ListItem>Visa</asp:ListItem>

                     </asp:RadioButtonList>
                  </td>
                  <td align="center" rowspan="1">
                     <asp:RequiredFieldValidator
                          id="RequiredFieldValidator1"
                          ControlToValidate="RadioButtonList1"
                          ErrorMessage="Card Type."
                          Display="Static"
                          InitialValue="" 
                          Width="100%" 
                          Text="*"
                          runat="server"/>
                  </td>
               </tr>
               <tr>
                  <td align="right">
                     Card Number:
                  </td>
                  <td>
                     <asp:TextBox id="TextBox1" 
                          runat="server" />
                  </td>
                  <td>
                     <asp:RequiredFieldValidator
                          id="RequiredFieldValidator2"
                          ControlToValidate="TextBox1" 
                          ErrorMessage="Card Number. "
                          Display="Static"
                          Width="100%"
                          Text="*" 
                          runat="server"/>
                  </td>
               </tr>

               <tr>
                  <td></td>
                  <td>
                     <asp:Button id="Button1" 
                          Text="Validate" 
                          runat="server" />
                  </td>
                  <td></td>
               </tr>
            </table>
         </td>
         <td valign="top">
            <table cellpadding="20">
               <tr>
                  <td>
                     <asp:ValidationSummary id="valSum" 
                          DisplayMode="BulletList"
                          EnableClientScript="true"
                          HeaderText="You must enter a value in the following fields:"
                          runat="server"/>
                  </td>
               </tr>
            </table>
         </td>
      </tr>
   </table>

</form>

See Also

Reference

ValidationSummary

Other Resources

Validation Server Control Syntax