Control.ClientID Control.ClientID Control.ClientID Control.ClientID Property


取得 ASP.NET 所產生之 HTML 標記的控制項識別碼。Gets the control ID for HTML markup that is generated by ASP.NET.

 virtual property System::String ^ ClientID { System::String ^ get(); };
public virtual string ClientID { get; }
member this.ClientID : string
Public Overridable ReadOnly Property ClientID As String


ASP.NET 所產生之 HTML 標記的控制項識別碼。The control ID for HTML markup that is generated by ASP.NET.


下列範例示範位於主版頁面的內容頁面的 Web 使用者控制項。The following examples show a Web user control that is inside a content page for a master page. 使用者控制項包含DropDownList控制項和Label控制項。The user control contains a DropDownList control and a Label control. 中所顯示的文字Label控制項由使用者從選取的值決定DropDownList控制項。The text that is displayed in the Label control is determined by the value that the user selects from the DropDownList control. 文字值是設定透過用戶端指令碼中,使網頁不需要回傳給伺服器,才能設定此值。The text value is set through client script so that the Web page does not have to post back to the server in order to set this value. 若要取得呈現的 HTML 元素的參考Label控制在用戶端指令碼,您必須知道控制項的值ClientID屬性。To get a reference to the HTML element that is rendered for the Label control in client script, you must know the value of the control's ClientID property. 不過,因為使用者控制項可以放在網頁上的任何位置,就無法事先知道哪些命名的容器將包含控制項。However, because the user control can be put anywhere in a Web page, it is impossible to know in advance which naming containers will contain the controls. 若要確定ClientID值將會是相同ID值,程式碼集ClientIDModeStaticTo make sure that the ClientID value will be the same as the ID value, the code sets the ClientIDMode value to Static.

下列範例會顯示使用者控制項。The following example shows the user control.

下列範例會顯示 [內容] 頁面包含使用者控制項。The following example shows the content page that contains the user control.

下列範例顯示包含 [內容] 頁面的主版頁面。The following example shows the master page that contains the content page.


當 Web 伺服器控制項轉譯成 HTML 元素, id HTML 項目的屬性設定的值為ClientID屬性。When a Web server control is rendered as an HTML element, the id attribute of the HTML element is set to the value of the ClientID property. ClientID值通常用來存取用戶端指令碼中的 HTML 項目使用document.getElementById方法。The ClientID value is often used to access the HTML element in client script by using the document.getElementById method. 識別碼通常也用於 CSS 規則以指定樣式的項目。The ID is also often used in CSS rules to specify elements to style. 例如,下列 CSS 樣式規則會選取所有span擁有的項目id屬性值ProductIDLabel並設定其background-color屬性設定為white:For example, the following CSS style rule selects all span elements that have the id attribute value of ProductIDLabel and sets their background-color attribute to white:

span#ProductIDLabel { background-color: white; }  

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 還舊的版本。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. 各區段是以底線字元 () 分隔。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、 模式會預設產生PredictableBecause the default for controls is Inherit, the default generation mode is Predictable. (不過,如果您使用 Visual Studio 將 Web 專案轉換成 ASP.NET 4 中,從較早版本時,Visual Studio 會自動將站台預設值設AutoIDWeb.config 檔案中。)(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.