ClientIDMode Enum

Definition

指定 ASP.NET 如何針對可在用戶端指令碼中存取的控制項產生 ClientIDSpecifies how ASP.NET generates the ClientID for a control that can be accessed in client script.

public enum class ClientIDMode
public enum ClientIDMode
type ClientIDMode = 
Public Enum ClientIDMode
Inheritance
ClientIDMode

Fields

AutoID 1

ClientID 值是透過串連每個父命名容器的 ID 值與控制項的 ID 值產生。The ClientID value is generated by concatenating the ID values of each parent naming container with the ID value of the control. 在呈現控制項之多個執行個體的資料繫結情節中,遞增值會插入控制項之 ID 值的前面。In data-binding scenarios where multiple instances of a control are rendered, an incrementing value is inserted in front of the control's ID value. 各區段是以底線字元 () 分隔。Each segment is separated by an underscore character (). 這是唯一可在比 ASP.NET 4 還舊的版本中使用的演算法。This algorithm is the only one that was available in versions of ASP.NET earlier than ASP.NET 4.

Inherit 0

此控制項會繼承其 ClientIDMode 控制項的 NamingContainer 設定。The control inherits the ClientIDMode setting of its NamingContainer control.

Predictable 2

這個演算法用於資料繫結控制項中的控制項。This algorithm is used for controls that are in data-bound controls. ClientID 值是透過串連父命名容器的 ClientID 值與控制項的 ID 值產生。The ClientID value is generated by concatenating the ClientID value of the parent naming container with the ID value of the control. 如果控制項是資料繫結控制項,並且會產生多個資料列,則 ClientIDRowSuffix 屬性中指定之資料欄位的值會加入結尾處。If the control is a data-bound control that generates multiple rows, the value of the data field specified in the ClientIDRowSuffix property is added at the end. 針對 GridView 控制項,可以指定多個資料欄位。For the GridView control, multiple data fields can be specified. 如果 ClientIDRowSuffix 是空屬性,則會在結尾處加入序號,而不是資料欄位值。If the ClientIDRowSuffix property is blank, a sequential number is added at the end instead of a data field value. 各區段是以底線字元 () 分隔。Each segment is separated by an underscore character ().

Static 3

ClientID 值設定為 ID 屬性的值。The ClientID value is set to the value of the ID property. 如果控制項是命名容器,則對於其包含的所有控制項而言,會用來做為命名容器之階層架構的最上層。If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.

Examples

下列範例會顯示 ListView 控制項中包含的 Label 控制項。The following example shows Label controls that are contained in a ListView control. ListView 控制項上,ClientIDMode 屬性會設定為可預測,而 ClientIDRowSuffix 屬性會設定為 ProductIDOn the ListView control, the ClientIDMode property is set to Predictable and the ClientIDRowSuffix property is set to ProductID. 您可以從用戶端腳本,根據您嘗試存取之實例的 ProductID 值,存取特定的 Label 實例。From client script, you can access a particular instance of a Label based on the ProductID value of the instance that you are trying to access.

這個範例會建立具有三個 ProductIDLabel 控制項的 HTML。This example creates HTML with three ProductIDLabel controls. 當頁面執行時,控制項的識別碼將會是下列內容:When the page runs, the IDs for the controls will be the following:

  • ListView1_ProductIDLabel_1
  • ListView1_ProductIDLabel_34
  • ListView1_ProductIDLabel_43
<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
                   XPath="Products/Product">
  <Data>
    <Products>
      <Product ProductID="1"  ProductName="Chai" />
      <Product ProductID="34" ProductName="Ale" />
      <Product ProductID="43" ProductName="Coffee" />
    </Products>
  </Data>
</asp:XmlDataSource>

<asp:ListView ID="ListView1" 
              ClientIDMode="Predictable" 
              ClientIDRowSuffix="ProductID"  
              DataSourceID="XmlDataSource1" runat="server" >
  <ItemTemplate>
    ProductID: 
    <asp:Label ID="ProductIDLabel" runat="server" 
               Text='<%# Eval("ProductID") %>' />
    <br />
    ProductName:
    <asp:Label ID="ProductNameLabel" runat="server" 
               Text='<%# Eval("ProductName") %>' />
    <br />
    <br />
  </ItemTemplate>

  <LayoutTemplate>
    <div ID="itemPlaceholderContainer" runat="server">
      <span ID="itemPlaceholder" runat="server" />
    </div>
    <div>
    </div>
  </LayoutTemplate>
  
</asp:ListView>

Remarks

當您設定控制項的 [ClientIDMode] 屬性時,會使用 ClientIDMode 列舉。You use the ClientIDMode enumeration when you set the ClientIDMode property for a control. 您指派給 ClientIDMode 屬性的值會決定產生 ClientID 屬性的方式。The value that you assign to the ClientIDMode property determines how the ClientID property is generated. ClientID 屬性的值會轉譯為標記中的 id 屬性,而這是您用來從用戶端腳本存取控制項的值。The value of the ClientID property is rendered as the id attribute in markup, and it is the value that you use to access the control from client script. 您指派給 ClientIDMode 屬性的值不會影響伺服器控制項的 ID 屬性,只有對應 HTML 元素的 id 屬性。The value that you assign to the ClientIDMode property does not affect the ID property of the server control, only the id attribute of the corresponding HTML element.

頁面的預設 ClientIDMode 值是可預測的。The default value of ClientIDMode for a page is Predictable. 控制項 ClientIDMode 的預設值為 [繼承]。The default value of ClientIDMode for a control is Inherit.

由於控制項的預設值是繼承的,因此預設的產生模式是可預測的。Because the default for controls is Inherit, the default generation mode is Predictable. (不過,如果您使用 Visual Studio 將 Web 專案從舊版轉換成 ASP.NET 4,Visual Studio 會自動將 web.config 檔案中的網站預設值設定為 Autoid predictable)。(However, if you use Visual Studio to convert a Web project to ASP.NET 4 from an earlier version, Visual Studio automatically sets the site default to AutoID in the Web.config file.)

您可以藉由設定網站 web.config 檔案中的pages元素,來設定網站中所有頁面的 ClientIDMode 值。You can set the ClientIDMode value for all pages in a Web site by setting the pages element in the site's Web.config file. 您可以在@ page指示詞中設定頁面的 ClientIDMode 值。You can set the ClientIDMode value for a page in the @ Page directive.

如需詳細資訊,請參閱 ASP.NET 網頁伺服器控制項識別For more information, see ASP.NET Web Server Control Identification.

Applies to

See also