ASP.NET 4.0配置文件中的ClientIDMode属性详解

2019-05-25 09:24:38王冬梅

<asp:GridView ID="grd_Account" runat="server" AllowPaging="True" 
  AutoGenerateColumns="False" 
  DataKeyNames="Account Number" DataSourceID="sds_account" Height="63px" 
  Width="676px" PageSize="5" ClientIDMode="Static" >
  <Columns>
    <asp:TemplateField HeaderText="Account Number" Sort ="Account Number">
      <ItemTemplate>
        <asp:LoginView ID="LoginView1" runat="server" ClientIDMode="Predictable" >
          <LoggedInTemplate>
            <asp:Label ID="Label1" runat="server" Text="Logged"></asp:Label>
          </LoggedInTemplate>
        </asp:LoginView>
      </ItemTemplate>        
    </asp:TemplateField>
  </Columns>
</asp:GridView>

可以看到在嵌套层次结构中,由于LoginView1所属的父容器控件grd_Account是显示多数据行的容器控件,所以LoginView1的ClientID应该是诸如:LoginView1_0、LoginView1_1等有ClientIDRowSuffix部分的格式,这没有问题,但是按道理来说Label1所在的父容器控件LoginView1不是显示多数据行的容器控件,那么Label1生成的ClientID 应该是诸如LoginView1_0_Label1、LoginView1_1_Label1等这样的没有ClientIDRowSuffix部分的格式,但是为我们来看一下生成的HTML代码:

<table cellspacing="0" rules="all" border="1" id="grd_Account" style="height:63px;width:676px;border-collapse:collapse;">
  <tr>
  <th scope="col">Account Number</th>
  </tr>
  <tr>
  <td>
      <span id="LoginView1_0_Label1_0">Logged</span>  
    </td>
  </tr>
  <tr>
  <td>
      <span id="LoginView1_1_Label1_1">Logged</span> 
    </td>
  </tr>
  <tr>
  <td>
      <span id="LoginView1_2_Label1_2">Logged</span>
    </td>
  </tr>
</table>

可以看到生成的Label1的ClientID都带表示递增行号的后缀字符串0、1、2等(此外请注意LoginView不会产生HTML代码),也就是有ClientIDRowSuffix部分。

我们再将grd_Account的ClientIDRowSuffix属性更改为数据源中的Account Number字段后再来看看生成的HTML代码:

<table cellspacing="0" rules="all" border="1" id="grd_Account" style="height:63px;width:676px;border-collapse:collapse;">
  <tr>
  <th scope="col">Account Number</th>
  </tr>
  <tr>
  <td> 
      <span id="LoginView1_1060_Label1_1060">Logged</span>   
    </td>
  </tr>
  <tr>
  <td> 
      <span id="LoginView1_1200_Label1_1200">Logged</span>   
    </td>
  </tr>
  <tr>
  <td>
      <span id="LoginView1_1510_Label1_1510">Logged</span>   
    </td>
  </tr>
</table>

可以看到生成的Label1的ClientID也都带ClientIDRowSuffix部分,只不过ClientIDRowSuffix部分现在是数据源中Account Number字段的值。