《C#4.0程序设计与项目实战》——验证控件

RequiredFieldValidator 控件

需要注意的是ErrorMessage与Text的异同,前者是专门为在 ValidationSummary 控件中显示文本,而后者则是验证失败时在本验证控件中显示的错误提示信息

特别讲到一点的是ValidationGroup属性,它的作用是使用验证组将页上的验证控件归入特定类别。每个验证组都可以独立于页上的其他验证组进行验证。使用 ValidationGroup 属性指定当控件回发到服务器时要进行验证的验证组的名称。示例程序如下:(下面的代码示例演示在 Button 控件回发到服务器时,如何使用 ValidationGroup 属性指定要验证的控件。该页包含三个用于获取用户数据的文本框和三个用于确保用户未将文本框留空的 RequiredFieldValidator 控件。前两个文本框的 RequiredFieldValidator 控件在 PersonalInfoGroup 验证组中,而第三个文本框的 RequiredFieldValidator 控件在LocationInfoGroup 验证组中。当单击 Button1 时,只会验证 PersonalInfoGroup 验证组中的控件。在单击 Button2 时,只验证 LocationInfoGroup 验证组中的控件。

View Code
<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>Button.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" runat="server">

    <h3>Button.ValidationGroup Example</h3>

    <asp:label id="NameLabel" 
      text="Enter your name:"
      runat="Server"
      AssociatedControlID="NameTextBox">
    </asp:label>

    &nbsp;

    <asp:textbox id="NameTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator1"
      controltovalidate="NameTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <asp:label id="AgeLabel" 
      text="Enter your age:"
      runat="Server" 
      AssociatedControlID="AgeTextBox">
    </asp:label>

    &nbsp;

    <asp:textbox id="AgeTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator2"
      controltovalidate="AgeTextBox"
      validationgroup="PersonalInfoGroup"
      errormessage="Enter your age."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <!--When Button1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:button id="Button1" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="PersonalInfoGroup"
      runat="Server" />

    <br /><br />

    <asp:label id="CityLabel" 
      text="Enter your city of residence:"
      runat="Server" 
      AssociatedControlID="CityTextBox">
    </asp:label>

    &nbsp;

    <asp:textbox id="CityTextBox" 
      runat="Server">
    </asp:textbox>

    &nbsp;

    <asp:requiredfieldvalidator id="RequiredFieldValidator3"
      controltovalidate="CityTextBox"
      validationgroup="LocationInfoGroup"
      errormessage="Enter a city name."
      runat="Server">
    </asp:requiredfieldvalidator>

    <br /><br />

    <!--When Button2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:button id="Button2" 
      text="Validate" 
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server" />

  </form>
</body>
</html>

 

属性描述
BackColor RangeValidator 控件的背景颜色
ControlToValidate 要验证的控件的 id
Display 验证控件的显示行为。

合法的值有:

  • None - 验证消息从不内联显示。
  • Static - 在页面布局中分配用于显示验证消息的空间。
  • Dynamic - 如果验证失败,将用于显示验证消息的空间动态添加到页面。
EnableClientScript 布尔值,规定是否启用客户端验证。
Enabled 布尔值,规定是否启用验证控件。
ErrorMessage

当验证失败时,在 ValidationSummary 控件中显示的文本。

注释:如果未设置 Text 属性,文本也会显示在该验证控件中。

ForeColor 该控件的前景色。
id 控件的唯一 id。
InitialValue 规定输入控件的初始值(开始值)。默认是 ""。
IsValid 布尔值,指示关联的输入控件是否通过验证。
runat 规定该控件是一个服务器控件。必须设置为 "server"。
Text 当验证失败时显示的消息。

 

ValidationSummary 控件

定义和用法

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

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

属性

属性描述
DisplayMode

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

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

ValidationSummary(验证总结)控件
  该控件收集本页的所有验证错误信息,并可以将它们组织以后再显示出来。其标准代码如下:

<ASP:ValidationSummary id="Validator_ID" RunAT="Server" HeaderText="头信息" ShowSummary="True|False" iaplayMode="List|BulletList|SingleParagraph">
</ASP: ValidationSummary >  

  在以上标准代码中,HeadText相当于表的HeadText,DisplayMode表示错误信息显示方式:List相当于HTML中的<BR>;BulletList相当于HTML中的<LI>;SingleParegraph表示错误信息之间不作如何分割;

CustomValidator(自定义验证)控件
  该控件用自定义的函数界定验证方式,其标准代码如下: 

<ASP:CustomValidator id="Validator_ID" RunAt="Server"
controlToValidate="要验证的控件"
onServerValidateFunction="验证函数"
errorMessage="错误信息"
Display="Static|Dymatic|None"

占位符
</ASP: CustomValidator >

  以上代码中,用户必须定义一个函数来验证输入。

posted @ 2012-12-07 08:25  .NET~莫愁  阅读(231)  评论(0编辑  收藏  举报