ASP.NET ValidationSummary 控件


定义和用法

ValidationSummary 控件用于显示网页中所有验证错误的摘要。

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

属性

属性描述
DisplayMode如何显示摘要。合法值有:
  • BulletList
  • List
  • SingleParagraph
EnableClientScript布尔值,规定是否启用客户端验证。
Enabled布尔值,规定是否启用验证控件。
ForeColor控件的前景颜色。
HeaderTextValidationSummary 控件中的标题文本。
id控件的唯一 id。
runat规定该控件是一个服务器控件。必须设置为 "server"。
ShowMessageBox布尔值,规定是否在消息框中显示验证摘要。
ShowSummary布尔值,规定 ValidationSummary 控件是否显示或者隐藏。

实例

Validationsummary

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form runat="server">
  6. <table>
  7. <tr>
  8. <td>
  9. <table bgcolor="#b0c4de" cellspacing="10">
  10.    <tr>
  11.      <td align="right">Name:</td>
  12.      <td><asp:TextBox id="txt_name" runat="server"/></td>
  13.      <td>
  14.      <asp:RequiredFieldValidator
  15.      ControlToValidate="txt_name" 
  16.      ErrorMessage="Name"
  17.      Text="*" 
  18.      runat="server"/>
  19.      </td>
  20.    </tr>
  21.    <tr>
  22.      <td align="right">Card Type:</td>
  23.      <td>
  24.      <asp:RadioButtonList id="rlist_type" 
  25.      RepeatLayout="Flow"
  26.      runat="server">
  27.      <asp:ListItem>Diners</asp:ListItem>
  28.      <asp:ListItem>MasterCard</asp:ListItem>
  29.      <asp:ListItem>Visa</asp:ListItem>
  30.      </asp:RadioButtonList>
  31.      </td>
  32.      <td>
  33.      <asp:RequiredFieldValidator
  34.      ControlToValidate="rlist_type"
  35.      ErrorMessage="Card Type"
  36.      InitialValue=""
  37.      Text="*"
  38.      runat="server"/>
  39.      </td>
  40.    </tr>
  41.    <tr>
  42.      <td></td>
  43.      <td><asp:Button id="b1" Text="Submit" runat="server"/></td>
  44.      <td></td>
  45.    </tr>
  46. </table>
  47. </td>
  48. </tr>
  49. </table>
  50. <br>
  51. <asp:ValidationSummary
  52. HeaderText="You must enter a value in the following fields:"
  53. DisplayMode="BulletList"
  54. EnableClientScript="true"
  55. runat="server"/>
  56. </form>
  57.  
  58. </body>
  59. </html>

在本例中,我们使用 ValidationSummary 控件生成了一个留空供用户填写的必填字段的列表。

Validationsummary 2

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <form runat="server">
  6. <table>
  7. <tr>
  8. <td>
  9. <table bgcolor="#b0c4de" cellspacing="10">
  10.    <tr>
  11.      <td align="right">Name:</td>
  12.      <td><asp:TextBox id="txt_name" runat="server"/></td>
  13.      <td>
  14.      <asp:RequiredFieldValidator
  15.      ControlToValidate="txt_name" 
  16.      ErrorMessage="Name"
  17.      Text="*" 
  18.      runat="server"/>
  19.      </td>
  20.    </tr>
  21.    <tr>
  22.      <td align="right">Card Type:</td>
  23.      <td>
  24.      <asp:RadioButtonList id="rlist_type" 
  25.      RepeatLayout="Flow"
  26.      runat="server">
  27.      <asp:ListItem>Diners</asp:ListItem>
  28.      <asp:ListItem>MasterCard</asp:ListItem>
  29.      <asp:ListItem>Visa</asp:ListItem>
  30.      </asp:RadioButtonList>
  31.      </td>
  32.      <td>
  33.      <asp:RequiredFieldValidator
  34.      ControlToValidate="rlist_type"
  35.      ErrorMessage="Card Type"
  36.      InitialValue=""
  37.      Text="*"
  38.      runat="server"/>
  39.      </td>
  40.    </tr>
  41.    <tr>
  42.      <td></td>
  43.      <td><asp:Button id="b1" Text="Submit" runat="server"/></td>
  44.      <td></td>
  45.    </tr>
  46. </table>
  47. </td>
  48. </tr>
  49. </table>
  50. <asp:ValidationSummary
  51. ShowMessageBox="true"
  52. ShowSummary="false"
  53. HeaderText="You must enter a value in the following fields:"
  54. EnableClientScript="true"
  55. runat="server"/>
  56. </form>
  57.  
  58. </body>
  59. </html>

在本例中,我们使用 ValidationSummary 控件显示了一个留空供用户填写的必填字段的消息框。