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 控件中包含的控件 ListViewThe 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 基于你尝试访问的实例的值访问的特定实例 ProductIDFrom 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:XmlDataSource ID="XmlDataSource1" runat="server" 
                   XPath="Products/Product">
  <Data>
    <Products>
      <Product ProductID="1"  ProductName="Chai" />
      <Product ProductID="34" ProductName="Ale" />
      <Product ProductID="43" ProductName="Coffee" />
    </Products>
  </Data>
</asp:XmlDataSource>

<asp:ListView ID="ListView1" 
              ClientIDMode="Predictable" 
              ClientIDRowSuffix="ProductID"  
              DataSourceID="XmlDataSource1" runat="server" >
  <ItemTemplate>
    ProductID: 
    <asp:Label ID="ProductIDLabel" runat="server" 
               Text='<%# Eval("ProductID") %>' />
    <br />
    ProductName:
    <asp:Label ID="ProductNameLabel" runat="server" 
               Text='<%# Eval("ProductName") %>' />
    <br />
    <br />
  </ItemTemplate>

  <LayoutTemplate>
    <div ID="itemPlaceholderContainer" runat="server">
      <span ID="itemPlaceholder" runat="server" />
    </div>
    <div>
    </div>
  </LayoutTemplate>
  
</asp:ListView>

注解

ClientIDMode 设置控件的属性时,可以使用枚举 ClientIDModeYou 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.

适用于

另请参阅