Validation服务器控件:ValidationSummary控件

2009-03-15 01:55:31  来源:网页教学网 

WebjxCom提示:ValidationSummary 控件用于在网页、消息框或在这两者中内联显示所有验证错误的摘要。

阅读此文请先查看网页教学网的:ASP.NET入门教程:Validation服务器控件,简单讲述了Web服务器控件的使用方法。

定义和用法

ValidationSummary 控件用于在网页、消息框或在这两者中内联显示所有验证错误的摘要。显示 Web 页上所有验证错误的列表。

在该控件中显示的错误消息是由每个验证控件的 ErrorMessage 属性规定的。如果未设置验证控件的 ErrorMessage 属性,就不会为那个验证控件显示错误消息。

属性

属性 描述
DisplayMode

如何显示摘要。合法值有:

  • BulletList
  • List
  • SingleParagraph
EnableClientScript 布尔值,规定是否启用客户端验证。
Enabled 布尔值,规定是否启用验证控件。
ForeColor 该控件的前景色。
HeaderText ValidationSummary 控件中的标题文本。
id 控件的唯一 id。
runat 规定该控件是一个服务器控件。必须设置为 "server"。
ShowMessageBox 布尔值,指示是否在消息框中显示验证摘要。
ShowSummary 布尔值,规定是否显示验证摘要。

实例

<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"
/>

备注:ValidationSummary 控件允许在单个位置概述网页上所有验证控件的错误信息。基于 DisplayMode 属性的值,该摘要可显示为列表、项目符号列表或单个段落。ValidationSummary 控件中为页面上每个验证控件显示的错误信息,是由每个验证控件的 ErrorMessage 属性指定的。如果没有设置验证控件的 ErrorMessage 属性,将不会在 ValidationSummary 控件中为该验证控件显示错误信息。通过设置 HeaderText 属性,还可以在 ValidationSummary 控件的标题部分指定一个自定义标题。

通过设置 ShowSummary 属性,可以控制 ValidationSummary 控件是显示还是隐藏。还可通过将 ShowMessageBox 属性设置为 true,在消息框中显示摘要。

示例

下面的代码示例演示如何使用 ValidationSummary 控件来概述页上的未通过验证的输入控件。

Visual Basic

<h3>ValidationSummary Sample</h3>
<p>
<form id="Form1" runat="server">
   <table cellpadding="10">
      <tr>
         <td>
            <table bgcolor="#eeeeee" cellpadding="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="middle" 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>

C#

<h3>ValidationSummary Sample</h3>
<p>
<form id="Form1" runat="server">
   <table cellpadding="10">
      <tr>
         <td>
            <table bgcolor="#eeeeee" cellpadding="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="middle" 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>

更多

推荐文章