指定验证组

更新:2007 年 11 月

使用验证组可以将页面上的验证控件归为一组。可以对每个验证组执行验证,该验证与同一页的其他验证组无关。

将要分组的所有控件的 ValidationGroup 属性设置为同一个名称(字符串)即可创建验证组。可以为验证组分配任何名称,但必须对该组的所有成员使用相同的名称。

在回发过程中,只根据当前验证组中的验证控件来设置 Page 类的 IsValid 属性。当前验证组是由导致验证发生的控件确定的。例如,如果单击验证组为 LoginForm 的按钮控件,并且其 ValidationGroup 属性设置为 LoginForm 的所有验证控件都有效,则 IsValid 属性将返回 true。对于其他控件(如 DropDownList 控件),如果控件的 CausesValidation 属性设置为 true(而 AutoPostBack 属性设置为 true),则也可以触发验证。

若要以编程方式进行验证,可以调用 Validate 方法重载,使其采用 validationGroup 参数来强制只为该验证组进行验证。请注意,在调用 Validate 方法时,IsValid 属性反映到目前为止已验证的所有组的有效性。这可能包括作为回发结果验证的组以及以编程方式验证的组。如果任一组中的任何控件无效,则 IsValid 属性返回 false。

下面的代码示例演示在 Button 控件回发到服务器时,如何使用 ValidationGroup 属性指定要验证的控件。页面包含三个文本框(用于从用户捕获数据)和三个 RequiredFieldValidator 控件(用于确保用户没有保留文本框为空白)。前两个文本框的 RequiredFieldValidator 控件位于 PersonalInfoGroup 验证组中,而第三个文本框的 RequiredFieldValidator 控件位于 LocationInfoGroup 验证组中。在单击 Button1 时,只验证 PersonalInfoGroup 验证组中的控件。在单击 Button2 时,只验证 LocationInfoGroup 验证组中的控件。

<%@ page language="VB" %>

<!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 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>
<%@ 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>

请参见

其他资源

验证 ASP.NET 控件