ClientIDMode 枚举

定义

指定 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
继承
ClientIDMode

字段

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 0

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

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 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. 在控件上ClientIDMode , 将属性ClientIDRowSuffix设置为可预测, 并将属性设置为ProductIDListViewOn 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.

此示例创建包含三个ProductIDLabel控件的 HTML。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 Server Control 标识For more information, see ASP.NET Web Server Control Identification

注解

在设置控件ClientIDModeClientIDMode属性时, 可以使用枚举。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 会自动将该站点默认设置为 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.)

通过在站点的ClientIDMode web.config 文件中设置pages元素, 你可以为网站中的所有页面设置值。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.

适用于

另请参阅