Control.ClientIDMode 屬性

定義

取得或設定用來產生 ClientID 屬性值的演算法。Gets or sets the algorithm that is used to generate the value of the ClientID property.

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

屬性值

值,指定如何產生 ClientID 屬性 (Property)。A value that indicates how the ClientID property is generated. 預設為 InheritThe default is Inherit.

屬性

範例

下列範例會顯示 ListView 控制項中包含的 Label 控制項。The following example shows Label controls that are contained in a ListView control. ListView 控制項上,ClientIDMode 屬性會設定為 Predictable 而且 ClientIDRowSuffix 屬性會設定為 [ProductID]。On the ListView control, the ClientIDMode property is set to Predictable and the ClientIDRowSuffix property is set to ProductID. 在呈現的 HTML 中,這會建立三個對應至三個 ProductIDLabel 控制項的 span 元素。In the rendered HTML, this creates three span elements that correspond to three ProductIDLabel controls. 當頁面執行時,span 元素的 id 屬性會設定為下列值:When the page runs, the id attributes for the span elements are set to the following values:

  • ListView1_ProductIDLabel_1

  • ListView1_ProductIDLabel_34

  • ListView1_ProductIDLabel_43

備註

ASP.NET 提供多個演算法來產生 ClientID 屬性值。ASP.NET provides multiple algorithms for how to generate the ClientID property value. 您可以藉由設定其 ClientIDMode 屬性,來選取要用於控制項的演算法。You select which algorithm to use for a control by setting its ClientIDMode property. 這些演算法是由下表所列的 ClientIDMode 列舉值所識別。The algorithms are identified by the ClientIDMode enumeration values that are listed in the following table.

Value 說明Description
AutoID 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 之前的 ASP.NET 版本中使用。This algorithm was used in versions of ASP.NET earlier than ASP.NET 4.
Static 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.
Predictable 這個演算法用於資料繫結控制項中的控制項。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. 這個數位會從零開始,而且每個資料列會遞增1。This number begins at zero and is incremented by 1 for each row. 各區段是以底線字元 () 分隔。Each segment is separated by an underscore character ().
Inherit 此控制項會繼承其 ClientIDMode 控制項的 NamingContainer 設定。The control inherits the ClientIDMode setting of its NamingContainer control.

頁面 ClientIDMode 的預設值是 PredictableThe default value of ClientIDMode for a page is Predictable. 控制項 ClientIDMode 的預設值為 InheritThe default value of ClientIDMode for a control is Inherit. 由於控制項的預設值是 Inherit,因此會 Predictable預設的產生模式。Because the default for controls is Inherit, the default generation mode is Predictable. (不過,如果您使用 Visual Studio 將 Web 專案從舊版轉換成 ASP.NET 4,Visual Studio 會自動將網站預設值設定為 web.config 檔案中的 AutoID)。(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.)

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

適用於

另請參閱