ClientIDMode 列舉

定義

指定 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
繼承
ClientIDMode

欄位

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.

範例

下列範例會顯示Label ListView控制項中包含的控制項。The following example shows Label controls that are contained in a ListView control. 在控制項上ClientIDMode , 屬性會ClientIDRowSuffix設定為可預測, 而屬性會設定為ProductIDListViewOn the ListView control, the ClientIDMode property is set to Predictable and the ClientIDRowSuffix property is set to ProductID. 您可以從用戶端腳本, 根據您嘗試存取的Label實例ProductID值來存取的特定實例。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.NET Web 服務器控制項識別For more information, see ASP.NET Web Server Control Identification

備註

當您設定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.)

您可以藉由ClientIDMode設定網站 web.config 檔案中的pages元素, 設定網站中所有頁面的值。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. 您可以在ClientIDMode @ page指示詞中設定頁面的值。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.

適用於

另請參閱