页面中引入
1
<%
@ Register TagPrefix
=
"
scott
"
TagName
=
"
header
"
Src
=
"
Controls/Header.ascx
"
%>
2 <% @ Register TagPrefix = " scott " TagName = " footer " Src = " Controls/Footer.ascx " %>
3 <% @ Register TagPrefix = " ControlVendor " Assembly = " ControlVendor " %>
4
5 < html >
6 < body >
7 < form id ="form1" runat ="server" >
8 < scott:header ID ="MyHeader" runat ="server" />
9 </ form >
10 </ body >
11 </ html >
注意到上面的前两个注册指令是用来注册用户控件的(是在.ascx文件里实现的),最后这个是用来注册编译进一个程序集 .dll 文件里的自定义控件的。注册完后,开发人员可以在页面的任何地方用设定好的 tagprefix (标识前缀)和标识符号名( tagname)来声明这些控件。
2 <% @ Register TagPrefix = " scott " TagName = " footer " Src = " Controls/Footer.ascx " %>
3 <% @ Register TagPrefix = " ControlVendor " Assembly = " ControlVendor " %>
4
5 < html >
6 < body >
7 < form id ="form1" runat ="server" >
8 < scott:header ID ="MyHeader" runat ="server" />
9 </ form >
10 </ body >
11 </ html >
优点:通过src属性,清晰直观的找到控件定义处;
缺点:如果你的项目有100个使用用户控件的页面.....,有一天改变了用户控件存放的目录
在Web.config中引入
<?
xml version="1.0"
?>
< configuration >
< system.web >
< pages >
< controls >
< add tagPrefix ="scottgu" src ="~/Controls/Header.ascx" tagName ="header" />
< add tagPrefix ="scottgu" src ="~/Controls/Footer.ascx" tagName ="footer" />
< add tagPrefix ="ControlVendor" assembly ="ControlVendorAssembly" />
</ controls >
</ pages >
</ system.web >
</ configuration >
优点:一旦声明便可在任意页面上通过
tagprefix和
tagname使用该控件;
< configuration >
< system.web >
< pages >
< controls >
< add tagPrefix ="scottgu" src ="~/Controls/Header.ascx" tagName ="header" />
< add tagPrefix ="scottgu" src ="~/Controls/Footer.ascx" tagName ="footer" />
< add tagPrefix ="ControlVendor" assembly ="ControlVendorAssembly" />
</ controls >
</ pages >
</ system.web >
</ configuration >
缺点:......
另外,对于自定义控件来说,存在 dynamic 与 non dynamic 之差:前者的类文件位于~/App_Code中,且无需指定assembly属性;后者一般将编译好的dll文件存放在~/bin目录,必须指定assembly属性。因此用户控件的assembly属性是可选的,namespace属性用于指定控件在程序集中所属的命名空间。
对于用户控件,必须指定tagName和src属性。