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.

public:
 virtual property System::String ^ ClientID { System::String ^ get(); };
[System.ComponentModel.Browsable(false)]
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.

下列範例會顯示包含使用者控制項的內容頁面。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屬性設定為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.

適用於

另請參閱