Control.ClientIDMode 属性

定义

获取或设置用于生成 ClientID 属性值的算法。Gets or sets the algorithm that is used to generate the value of the ClientID property.

public:
 virtual property System::Web::UI::ClientIDMode ClientIDMode { System::Web::UI::ClientIDMode get(); void set(System::Web::UI::ClientIDMode value); };
[System.Web.UI.Themeable(false)]
public virtual System.Web.UI.ClientIDMode ClientIDMode { get; set; }
member this.ClientIDMode : System.Web.UI.ClientIDMode with get, set
Public Overridable Property ClientIDMode As ClientIDMode

属性值

一个值,指示如何生成 ClientID 属性。A value that indicates how the ClientID property is generated. 默认值为 InheritThe default is Inherit.

属性

示例

下面的示例演示 Label 包含在 ListView 控件中的控件。The following example shows Label controls that are contained in a ListView control. ListView 控件上,ClientIDMode 属性设置为 PredictableClientIDRowSuffix 属性设置为 ProductIDOn the ListView control, the ClientIDMode property is set to Predictable and the ClientIDRowSuffix property is set to ProductID. 在呈现的 HTML 中,这将创建三个对应于三个 ProductIDLabel 控件的 span 元素。In the rendered HTML, this creates three span elements that correspond to three ProductIDLabel controls. 当页面运行时,span 元素的 id 属性将设置为以下值:When the page runs, the id attributes for the span elements are set to the following values:

  • ListView1_ProductIDLabel_1

  • ListView1_ProductIDLabel_34

  • ListView1_ProductIDLabel_43

注解

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. 此数字从零开始,每一行递增1。This number begins at zero and is incremented by 1 for each row. 各部分之间用下划线字符 () 分隔。Each segment is separated by an underscore character ().
Inherit 控件继承其 NamingContainer 控件的 ClientIDMode 设置。The control inherits the ClientIDMode setting of its NamingContainer control.

页面 ClientIDMode 的默认值为 "Predictable"。The 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 Web 服务器控件标识For more information, see ASP.NET Web Server Control Identification.

适用于

另请参阅