ClientIDMode ClientIDMode ClientIDMode ClientIDMode Enum

定义

指定 ASP.NET 如何为客户端脚本中可以访问的控件生成 ClientIDSpecifies how ASP.NET generates the ClientID for a control that can be accessed in client script.

public enum class ClientIDMode
public enum ClientIDMode
type ClientIDMode = 
Public Enum ClientIDMode
继承

字段

AutoID AutoID AutoID AutoID 1

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 is the only one that was available in versions of ASP.NET earlier than ASP.NET 4.

Inherit Inherit Inherit Inherit 0

控件继承其 NamingContainer 控件的 ClientIDMode 设置。The control inherits the ClientIDMode setting of its NamingContainer control.

Predictable Predictable Predictable Predictable 2

对于数据绑定控件中的控件使用此算法。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 ().

Static Static Static Static 3

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.

示例

下面的示例演示Label中包含的控件ListView控件。The following example shows Label controls that are contained in a ListView control. ListView控件,ClientIDMode属性设置为可预测和ClientIDRowSuffix属性设置为ProductIDOn the ListView control, the ClientIDMode property is set to Predictable and the ClientIDRowSuffix property is set to ProductID. 可以从客户端脚本访问的特定实例Label基于ProductID你尝试访问的实例的值。From client script, you can access a particular instance of a Label based on the ProductID value of the instance that you are trying to access.

此示例将创建具有三个 HTMLProductIDLabel控件。This example creates HTML with three ProductIDLabel controls. 页运行时,控件的 Id 将是以下:When the page runs, the IDs for the controls will be the following:

  • ListView1_ProductIDLabel_1

  • ListView1_ProductIDLabel_34

  • ListView1_ProductIDLabel_43

有关详细信息,请参阅ASP.NET Web 服务器控件标识For more information, see ASP.NET Web Server Control Identification

注解

您使用ClientIDMode枚举设置时ClientIDMode控件的属性。You use the ClientIDMode enumeration when you set the ClientIDMode property for a control. 将分配到的值ClientIDMode属性确定如何ClientID生成属性。The value that you assign to the ClientIDMode property determines how the ClientID property is generated. ClientID属性将呈现为id中标记,和它的属性是用于从客户端脚本访问该控件的值。The value of the ClientID property is rendered as the id attribute in markup, and it is the value that you use to access the control from client script. 将分配到的值ClientIDMode属性不会影响ID属性的服务器控件,仅id对应的 HTML 元素的属性。The value that you assign to the ClientIDMode property does not affect the ID property of the server control, only the id attribute of the corresponding HTML element.

默认值ClientIDMode页是可预测。The default value of ClientIDMode for a page is Predictable. 默认值ClientIDMode控件是继承。The default value of ClientIDMode for a control is Inherit.

控件的默认值为继承,因为默认生成模式下是可预测。Because the default for controls is Inherit, the default generation mode is Predictable. (但是,如果您使用 Visual Studio 将 Web 项目转换为 ASP.NET 4 中,从早期版本,Visual Studio 会自动设置站点默认值为 AutoID Web.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.)

可以设置ClientIDMode值设置为 Web 站点中的所有页面页面站点的 Web.config 文件中的元素。You can set the ClientIDMode value for all pages in a Web site by setting the pages element in the site's Web.config file. 可以设置ClientIDMode值中的页@ Page指令。You can set the ClientIDMode value for a page in the @ Page directive.

有关详细信息,请参阅 ASP.NET Web 服务器控件标识For more information, see ASP.NET Web Server Control Identification.

适用于

另请参阅