ValidationSummary控件是用于显示验证所有验证错误摘要的控件,当我们将验证控件的 Display属性设置 None 的时候,验证错误信息就在这里显示。
ValidationSummary控件有三个常见属性:
属性名
说明
DisplayMode
指定显示模式,有 BulletList、 List、 SingleParagraph 三种模式ShowMessageBox
是否以客户端提示框的信息显示验证错误信息摘要
ShowSummary
是否在网页中采用内联方式显示错误摘要
下面是一个ValidationSummary 控件的例子(在使用 ValidationSummary控件的时候一定要将验证控件的 Display 属性设为 None):
运行效果
[HTML] 纯文本查看 复制代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ValidationSummaryDemo.aspx.cs" Inherits="验证控件.ValidationSummaryDemo" %>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ValidationSummary控件的例子</title>
</head>
<body>
<form id="form1" runat="server">
<p>
<table border="1" width="600">
<tr>
<td colspan="2" align="center">旅游活动申请表</td>
</tr>
<tr>
<td>开始时间</td>
<td>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"ControlToValidate="txtStartDate"
ErrorMessage="用户名必须填写" Display="None"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="开始日期必须早于结束日期"ControlToCompare="txtStartDate" ControlToValidate="txtEndDate"Operator="GreaterThanEqual" Type="Date"
Display="None"></asp:CompareValidator></td>
</tr>
<tr>
<td>结束时间</td>
<td>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEndDate"ErrorMessage="请填写结束时间" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>参加人数</td>
<td>
<asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"ControlToValidate="txtNumber"
ErrorMessage="必须填写人数" Display="None"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"ControlToValidate="txtNumber"
ErrorMessage="人数必须大于0" Operator="GreaterThan" Type="Integer"ValueToCompare="0" Display="None"></asp:CompareValidator></td>
</tr>
<tr>
<td>经费预算</td>
<td>
<asp:TextBox ID="txtMoney" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator3" runat="server" ControlToValidate="txtMoney"ErrorMessage="经费必须是大于0的数字" Type="Currency" ValueToCompare="0" Display="None" Operator="GreaterThan"></asp:CompareValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button2" runat="server" Text="提交" />
</td>
<td>
<input id="Reset3" type="reset" value="重置" /></td>
</tr>
</table>
</p>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="SingleParagraph" ShowSummary="false" />
</form>
</body>
</html>
|