指定驗證群組

更新:2007 年 11 月

驗證群組可讓您將頁面上的驗證控制項組織成一組,每個驗證群組都能獨立執行驗證,而與頁面上的其他驗證群組無關。

若要建立驗證群組,請將所有您想要群組之控制項的 ValidationGroup 屬性設定為相同名稱 (字串)。您可以指派任何名稱給驗證群組,不過群組的所有成員都必須使用相同名稱。

在回傳期間,Page 類別 (Class) 之 IsValid 屬性只會根據目前驗證群組中之驗證控制項來設定。目前驗證群組是由造成驗證發生的控制項所決定。例如,當按一下具有 LoginForm 之驗證群組的按鈕控制項時,如果所有 ValidationGroup 屬性設定為 LoginForm 的驗證控制項都有效,則 IsValid 屬性會傳回 true。如果其他控制項 (例如 DropDownList 控制項) 的 CausesValidation 屬性設定為 true (且 AutoPostBack 屬性設定為 true),則該控制項也可能會觸發 (Trigger) 驗證。

若要以程式設計方式進行驗證,您可以呼叫使用 validationGroup 參數的 Validate 方法多載,以便只針對該驗證群組強制執行驗證。請注意,當您呼叫 Validate 方法時,IsValid 屬性會反映至今已驗證之所有群組的有效性。這可包含由於回傳而驗證的群組,以及以程式設計方式驗證的群組。如果任一群組中的任何控制項無效,則 IsValid 屬性會傳回 false。

在下列程式碼範例中,示範了如何使用 ValidationGroup 屬性,指定要在 Button 控制項回傳至伺服器時驗證的控制項。該頁面包含三個擷取使用者之資料的文字方塊,以及三個確保使用者不會將文字方塊保留空白的 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 >
  <title>Button.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" >

    <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" >
  <title>Button.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" >

    <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 控制項