2011-01-30 49 views

回答

17

@Register是较为常用的指令。当你想以声明方式在aspx或ascx页面中使用用户控件时,可以使用它。 @Register将控件与特定的前缀相关联,然后可以在标记中使用它。

@Reference仅告诉ASP.NET在编译aspx或ascx页面时编译其他控件。这可以确保它在运行时可用,并且可以通过编程方式添加到控制层次结构中。这是不常见的,因为在运行时动态更改用户控件不是comon。

这是一篇很好的博客文章。

http://weblogs.asp.net/johnkatsiotis/archive/2008/08/13/the-reference-directive.aspx

20

@Register主要用于登记标记前缀页面内声明使用的控制。

<%@ Register tagprefix="my" namespace="MyNamespace" %> 

<my:CustomControl runat=server /> 

@Reference主要用于指一个页面或用户控件(通过文件名或虚拟路径)编程成员的页面或控制的

<%@ Reference Control="MyControl.ascx" %> 

<% MyControl ctrl = (MyControl) Page.LoadControl("MyControl.ascx"); 
    ctrl.CustomProperty = "..."; //REFERENCE directive is needed to access property 
%>