共用方式為


ImageButton.ValidationGroup 屬性

定義

取得或設定控制項群組,ImageButton 控制項會在回傳至伺服器時,針對這個群組進行驗證。

public:
 virtual property System::String ^ ValidationGroup { System::String ^ get(); void set(System::String ^ value); };
[System.Web.UI.Themeable(false)]
public virtual string ValidationGroup { get; set; }
[<System.Web.UI.Themeable(false)>]
member this.ValidationGroup : string with get, set
Public Overridable Property ValidationGroup As String

屬性值

控制項群組,ImageButton 控制項會在回傳至伺服器時,針對這個群組進行驗證。 預設值為空字串 ("")。

實作

屬性

範例

下列程式代碼範例示範如何使用 ValidationGroup 屬性來指定控制項在控件回傳至伺服器時 ImageButton 驗證的控制件。 頁面包含三個文本框,可從使用者擷取數據,以及三 RequiredFieldValidator 個控件,以確保使用者不會將文本框保留空白。 前 RequiredFieldValidator 兩個文本框的控件位於驗證群組中 PersonalInfoGroupRequiredFieldValidator 而第三個文本框的控件位於驗證群組中 LocationInfoGroup 。 按兩下時 ImageButton1 ,只會驗證驗證群組 PersonalInfoGroup 中的控制件。 按兩下 時 ImageButton2 ,只會驗證驗證群組 LocationInfoGroup 中的控制件。

<%@ 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>ImageButton.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" runat="server">
  
    <h3>ImageButton.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 ImageButton1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:imagebutton id="ImageButton1" 
      alternatetext="Validate PersonalInfoGroup controls" 
      imageurl="Images/ImageButton1.JPG"
      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 ImageButton2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:imagebutton id="ImageButton2" 
      alternatetext="Validate LocationInfoGroup controls" 
      imageUrl="Images/ImageButton2.JPG"
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server" />

  </form>
</body>
</html>
<%@ 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 id="head1" runat="server">
  <title>ImageButton.ValidationGroup Example</title>
</head>
<body>
  <form id="form1" runat="server">
  
    <h3>ImageButton.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 ImageButton1 is clicked, only validation
    controls that are a part of PersonalInfoGroup
    are validated.-->
    <asp:imagebutton id="ImageButton1" 
      alternatetext="Validate PersonalInfoGroup controls" 
      imageurl="Images/ImageButton1.JPG"
      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 ImageButton2 is clicked, only validation
    controls that are a part of LocationInfoGroup
    are validated.-->
    <asp:imagebutton id="ImageButton2" 
      alternatetext="Validate LocationInfoGroup controls" 
      imageUrl="Images/ImageButton2.JPG"
      causesvalidation="true"
      validationgroup="LocationInfoGroup"
      runat="Server" />

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

備註

驗證群組可讓您將頁面上的驗證控件指派給特定類別。 每個驗證群組都可以獨立於頁面上的其他驗證群組進行驗證。 ValidationGroup使用屬性可指定控制項在回傳至伺服器時造成驗證的驗證組ImageButton名。 只有當 屬性的值設定為 true時,CausesValidation這個屬性才會生效。 當您指定 屬性的值 ValidationGroup 時,只有在控件回傳至伺服器時 ImageButton ,才會驗證屬於指定群組一部分的驗證控件。 如果您未指定這個屬性的值,而且 CausesValidation 屬性設定為 true,則控件回傳至伺服器時,會驗證頁面上未指派給驗證群組的所有驗證控件。

這個屬性無法由佈景主題或樣式表主題設定。 如需詳細資訊,請參閱 ThemeableAttributeASP.NET 主題和外觀

適用於

另請參閱