none
Predictable ID的后缀的作用? RRS feed

  • 问题

  • Default2.aspx

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
    <%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc1" TagName="WebUserControl" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
    		<asp:Repeater ID="Repeater1" runat="server">
    			<ItemTemplate>
    				<div runat="server" id="myDiv">
    					<uc1:WebUserControl runat="server" ID="WebUserControl" />
    				</div>
    			</ItemTemplate>
    		</asp:Repeater>
        </form>
    </body>
    </html>

    WebUserControl.ascx

    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>
    
    <asp:Label ID="myLabel" runat="server">你好</asp:Label>

    这样生成的HTML代码是

    <div id="Repeater1_myDiv_0">
    	<span id="Repeater1_WebUserControl_0_myLabel_0">你好</span>
    </div>
    
    <div id="Repeater1_myDiv_1">
    	<span id="Repeater1_WebUserControl_1_myLabel_1">你好</span>
    </div>
    

    我的ClientIDMode是Predictable,为什么span id的myLabel后面还会附加数字,如上面代码的粗体部分?

    我觉得Repeater1_WebUserControl_0_myLabel和Repeater1_WebUserControl_1_myLabel已经可以区分了呀。

    我发现这个后缀是Control.GetPredictableClientIDSuffix()添加的,但是为什么需要有这段代码?为什么在myLabel后面还需要添加父命名容器的DisplayIndex?能否给出一个例子,在不添加suffix时两个控件会有相同client id?

    2016年7月11日 19:05

答案

  • Predictable的机制

    当控件的ClientIDMode选中为Predictable时,该控件的ClientID 值是通过串联父容器控件(诸如GridView、ListView、LoginView等就是容器性控件)的 ClientID 值生成的。另外如果该控件是在显示多个数据行的父容器控件或祖先容器控件中(例如GridView、ListView就是显示多数据行的容器控件),则还会在该控件ClientID 值的末尾添加 ClientIDRowSuffix 属性中指定的数据字段的值。 对于 GridView 控件,ClientIDRowSuffix 属性可以指定多个数据字段。 如果 ClientIDRowSuffix 属性为空白,则在末尾添加递增的行号,而非数据字段值。 各部分之间以下划线字符 (_) 分隔。

    参考  http://www.cnblogs.com/OpenCoder/archive/2010/11/23/1885347.html


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2016年7月12日 0:01