Control.ClientIDMode 屬性


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

 virtual property System::Web::UI::ClientIDMode ClientIDMode { System::Web::UI::ClientIDMode get(); void set(System::Web::UI::ClientIDMode value); };
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.



下列範例會顯示 Label 控制項中包含的控制項 ListViewThe following example shows Label controls that are contained in a ListView control. ListView 控制項上, ClientIDMode 屬性會設定為, PredictableClientIDRowSuffix 屬性會設定為 ProductIDOn the ListView control, the ClientIDMode property is set to Predictable and the ClientIDRowSuffix property is set to ProductID. 在轉譯的 HTML 中,這會建立三個 span 對應至三個控制項的元素 ProductIDLabelIn the rendered HTML, this creates three span elements that correspond to three ProductIDLabel controls. 當頁面執行時, id 元素的屬性 span 會設定為下列值: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:XmlDataSource ID="XmlDataSource1" runat="server" 
      <Product ProductID="1"  ProductName="Chai" />
      <Product ProductID="34" ProductName="Ale" />
      <Product ProductID="43" ProductName="Coffee" />

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

    <div ID="itemPlaceholderContainer" runat="server">
      <span ID="itemPlaceholder" runat="server" />


ASP.NET 提供多個演算法來產生 ClientID 屬性值。ASP.NET provides multiple algorithms for how to generate the ClientID property value. 您可以藉由設定屬性來選取要用於控制項的演算法 ClientIDModeYou 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.

頁面的預設值 ClientIDModePredictableThe default value of ClientIDMode for a page is Predictable. 控制項的預設值 ClientIDModeInheritThe default value of ClientIDMode for a control is Inherit. 由於控制項的預設值為 Inherit ,因此預設的產生模式為 PredictableBecause 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.