我们在用ASP.NET写出来的网页,用浏览器来查看生成的客户端代码的时候经常看到这样的代码:GridView1_ctl101_WebUserControl1_webuserControlButton,那么这个命名有什么规律,是怎么来的拉?本次我们使用Reflector查看.net的代码研究其中的规律。

我们的ASP.NET服务器端控件在生成客户端控件的时候一般有id 和name两个属性,这两个属性我们在服务器端可以通过ClientID和UniqueID来得到。
以一个Button为例,用Reflector打开System.Web.dll,找到System.Web.UI.WebControls命名空间下面的Button类,我们可以发现该类继承至WebControl类,其实大多数控件都继承至这个类。这个类是继承了System.Web.UI.Control类的,这个类是我们要研究的重点,该类继承至System.Object类,这是所有类的基类,我们就不去研究了,接下来我们来看看Control类。
找到Control类下面的ClientID属性,查看其代码如下:

public virtual string ClientID
{
    get
    {
        this.EnsureID();
        string uniqueID = this.UniqueID;
        if ((uniqueID != null) && (uniqueID.IndexOf(this.IdSeparator) >= 0))
        {
            return uniqueID.Replace(this.IdSeparator, '_');
        }
        return uniqueID;
    }
}

也就是说ClientID就是将UniqueID中的IdSeparator (其值为:”$”)替换为”_”。比如我们写一个页面生成出来的代码如下:
<input type="submit" name="GridView1$ctl101$WebUserControl1$webuserControlButton" value="Button" id="GridView1_ctl101_WebUserControl1_webuserControlButton" />
显然name和id的不同就是将其中$替换为了_。
现在ClientID我们已经清楚了,那么UniqueID又是怎么生成的拉?让我们用Reflector来看看。

public virtual string UniqueID
{
    get
    {
        if (this._cachedUniqueID == null)
        {
            Control namingContainer = this.NamingContainer;//获得父控件
            if (namingContainer == null)
            {
                return this._id;
            }
            if (this._id == null)
            {
                this.GenerateAutomaticID();//对控件自动编号
            }
            if (this.Page == namingContainer)//当前控件的父控件是Page则UniqueID就是控件的ID。
            {
                this._cachedUniqueID = this._id;
            }
            else//当前控件父控件是另一种容器控件
            {
                string uniqueIDPrefix = namingContainer.GetUniqueIDPrefix();//取得父控件UniqueID+分隔符($)作为当前控件的UniqueID前缀。
                if (uniqueIDPrefix.Length == 0)
                {
                    return this._id;
                }
                this._cachedUniqueID = uniqueIDPrefix + this._id;//前缀+ID 作为当前控件的UniqueID
            }
        }
        return this._cachedUniqueID;
    }
}

这段代码中,最重要的就是GenerateAutomaticID()函数和namingContainer.GetUniqueIDPrefix();函数。我们可以跟进去看看函数是如何实现的。

private void GenerateAutomaticID()
{
    this.flags.Set(0x200000);
    this._namingContainer.EnsureOccasionalFields();
    int index = this._namingContainer._occasionalFields.NamedControlsID++;
    if (this.EnableLegacyRendering)
    {
        this._id = "_ctl" + index.ToString(NumberFormatInfo.InvariantInfo);
    }
    else if (index < 0x80)
    {
        this._id = automaticIDs[index];
    }
    else
    {
        this._id = "ctl" + index.ToString(NumberFormatInfo.InvariantInfo);
    }
    this._namingContainer.DirtyNameTable();
}

从这个函数我们可以看出,对于像GridView这种绑定控件,其生成的每一行中的控件名是由ctl+自增的数字组成的。其中数字的格式化是两位数字,也就是说不足两位的时候补零,多出两位就按实际内容算。

internal virtual string GetUniqueIDPrefix()
{
    this.EnsureOccasionalFields();
    if (this._occasionalFields.UniqueIDPrefix == null)
    {
        string uniqueID = this.UniqueID;
        if (!string.IsNullOrEmpty(uniqueID))
        {
            this._occasionalFields.UniqueIDPrefix = uniqueID + this.IdSeparator;
        }
        else
        {
            this._occasionalFields.UniqueIDPrefix = string.Empty;
        }
    }
    return this._occasionalFields.UniqueIDPrefix;
}

这个函数返回父控件的UniqueID+IdSeparator,如果父控件UniqueID为空,那么就返回空。
现在我们再回过头来看看GridView1$ctl101$WebUserControl1$webuserControlButton这命名:从中我们可以看到这是一个GridView控件下面绑定了一个WebUserControl控件,而这个控件中有一个webuserControlButton控件。Asp.Net控件的客户端命名

Asp.Net控件的客户端命名的更多相关文章

  1. asp.net控件开发基础(1)(转)原文更多内容

    asp.net本身提供了很多控件,提供给我们这些比较懒惰的人使用,我认为控件的作用就在此,因为我们不想重复工作,所以要创建它,这个本身便是一个需求的关系,所以学习控件开发很有意思. wrox网站上有本 ...

  2. 服务器端控件的"客户端"

    控件的服务端ID和客户端ID 比如一个ID为TextBox1的服务器端控件,在客户端访问该控件的DOM元素时 错误: var txtbox=document.getElementByID(" ...

  3. ASP.NET控件<ASP:Button /> html控件<input type="button">区别联系

    ASP.NET控件<ASP:Button />-------html控件<input type="button">杨中科是这么说的:asp和input是一样 ...

  4. asp.net <asp:Content>控件

    <asp:Content ID="Content2" ContentPlaceHolderID="CPH_MainContent" runat=" ...

  5. FineUI 基于 ExtJS 的专业 ASP.NET 控件库

    FineUI 基于 ExtJS 的专业 ASP.NET 控件库 http://www.fineui.com/

  6. asp.net控件的Hyperlink控件

    Asp.net控件: Hyperlink控件:Hyperlink控件又称为超链接控件,该控件在功能上跟Html的<a herf=””>控件相似,其显示的模式为超链接的形式. 注意: Hyp ...

  7. C# 控件缩写大全+命名规范+示例

    如有转载,请注明出处:http://www.cnblogs.com/flydoos/archive/2011/08/29/2158903.html C# 控件缩写大全+命名规范+示例 写程序的时候突然 ...

  8. 把某个asp.net 控件 替换成 自定义的控件

    功能:可以把某个asp.net 控件 替换成 自定义的控件 pages 的 tagMapping 元素(ASP.NET 设置架构) 定义一个标记类型的集合,这些标记类型在编译时重新映射为其他标记类型. ...

  9. Asp.Netserver控件开发的Grid实现(三)列编辑器

    以下是GridColumnsEditor的实现代码: GridColumnsEditor.cs using System; using System.Collections.Generic; usin ...

随机推荐

  1. 2016/3/16 45道MySQL 查询练习题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  2. e3 cpu

    英特尔® 至强® E3 处理器 https://www.intel.cn/content/www/cn/zh/products/processors/xeon/e3-processors.html?p ...

  3. kernel: audit: printk limit exceeded

    问题: 小长假的第一天早上8:18一个数据,被定时任务中的脚本漏处理: 查定时任务的日志,发现调度异常 查var messages-20171231 日志信息,排查问题. http://man7.or ...

  4. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  5. Powershell 常见问题

    unapproved verbs WARNING: The names of some imported commands from the module 'todo' include unappro ...

  6. mysql数据库ip与字符串

    Mysql自带的IP转换语句 inet_aton:将ip地址转换成数字型 inet_ntoa:将数字型转换成ip地址 //使用inet_aton函数,将字符串IP转换为整型: mysql> se ...

  7. Mybatis用到的设计模式和常用类原理

    功能:将java对象映射城sql语句,将结果集转化成java对象.将容易变化的放在配置文件中,不变的通过Mybatis管理. 完成:1.根据JDBC规范建立数据库的连接: 2.通过反射打通java对象 ...

  8. kafka-net

    基于kafka-net实现的可以长链接的消息生产者 今天有点时间,我就来说两句.最近接触的Kafka相关的东西要多一些,其实以前也接触过,但是在项目使用中的经验不是很多.最近公司的项目里面使用了Kaf ...

  9. mysql数据恢复失败记录

    今天遇到了MySQL有几个数据表空间丢失的问题,作为一个外行尝试好久没恢复成功,考虑到只是几个基础数据表,就删除数据表停止服务,删除ibd文件后再创新创建表解决了问题. 近期的一些事让我不像以前一样钻 ...

  10. center os 安装mysql5.6

    软件 MySQL-server-5.6.13-1.el6.x86_64.rpm MySQL-client-5.6.13-1.el6.x86_64.rpm 安装命令 rpm -ivh MySQL-ser ...