閱讀此文請先查看網頁教學網的:ASP.NET入門教程:Validation服務器控件,簡單講述了Web服務器控件的使用方法。
ValidationSummary 控件用於在網頁、消息框或在這兩者中內聯顯示所有驗證錯誤的摘要。顯示 Web 頁上所有驗證錯誤的列表。
在該控件中顯示的錯誤消息是由每個驗證控件的 ErrorMessage 屬性規定的。如果未設置驗證控件的 ErrorMessage 屬性,就不會為那個驗證控件顯示錯誤消息。
如何顯示摘要。合法值有:
<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>