Control.ClientID 屬性


取得 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. 若要取得針對用戶端腳本中的 Label 控制項所呈現之 HTML 專案的參考,您必須知道控制項的 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 值相同,程式碼會將 ClientIDMode 值設定為 StaticTo 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.

<%@ Control AutoEventWireup="true" %>

<script type="text/javascript">
  var seasonalSports = new Array("None selected",

  function DisplaySport(x) {
      = seasonalSports[x];

<asp:DropDownList ID="DropDownList1" runat="server" 
  <asp:ListItem Value="Select a season"></asp:ListItem>
  <asp:ListItem Value="Spring"></asp:ListItem>
  <asp:ListItem Value="Summer"></asp:ListItem>
  <asp:ListItem Value="Autumn"></asp:ListItem>
  <asp:ListItem Value="Winter"></asp:ListItem>
<br />
<asp:Label ID="SelectedSport" runat="server" ClientIDMode="Static">

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

<%@ Page Title="" MasterPageFile="~/Seasons.master" AutoEventWireup="true" %>

<%@ Register Src="Seasons.ascx" TagName="Seasons" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
  <uc1:Seasons ID="Seasons1" runat="server" />

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

<%@ Master AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <asp:ContentPlaceHolder id="head" runat="server">
    <form id="form1" runat="server">
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">


當 Web 服務器控制項轉譯為 HTML 元素時,HTML 專案的 id 屬性會設定為 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 樣式規則會選取所有具有 id 屬性值 ProductIDLabelspan 專案,並將其 background-color 屬性設定為 whiteFor 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 之前的 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. 各區段是以底線字元 () 分隔。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.