INamingContainer接口 INamingContainer接口是组合控件需要继承的一个接口,但它具体有什么作用呢? INamingContainer 是一个没有方法的标记接口。当控件在实现INamingContainer 时,ASP.NET 页框架可在该控件下创建新的命名范围,因此能够确保子控件在控件的分层树中具有唯一的名称。当复合控件公开模板属性,提供数据绑定或需要传送事件到子控件时,这是非常重要的。 简单的说就是为子控件id加一个前缀,前缀名就是父控件名称;比如在formview1下的模板列中加入一个label1,那么在生成的页面中,改label做为formview1的子控件,其id被改为了formview1_label1,作用是为了保证其id的唯一性 请看实例: 有这样一个组合控件:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace CommandButton
>
在没有继承INamingContainer接口的情况下: 继承了INamingContainer接口:
public class CommandButton : Control, INamingContainer
ps:一个小技巧,在页面上引用该控件时,不用对其注册,需要以下两个步骤: 1.在AssemblyInfo.cs里面
using System.Web.UI; [assembly: TagPrefix( " SeyonControls.CommandButton " , " Seyon " )]//SeyonControls.CommandButton是命名空间,Seyon是控件前面的tag
2.在web.config里面添加
< pages > < controls > < add tagPrefix ="Seyon" assembly ="CommandButton" namespace ="SeyonControls.CommandButton" /> </ controls > </ pages >
这样在页面中就可以直接使用,不用每次都注册
>
相关文章: