简体   繁体   English

ASP.NET @Register vs. @Reference

[英]ASP.NET @Register vs. @Reference

I'm working with referencing user controls on my ASPX page and I'm wondering what the difference is between these two page directives. 我正在我的ASPX页面上引用用户控件,我想知道这两个页面指令之间的区别。

@Reference @Register @Reference @Register

@Register is primarily used for registering tag prefixes to declaratively use controls within a page. @Register主要用于注册标记前缀声明性地使用页面内的控件。

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

<my:CustomControl runat=server />

@Reference is primarily used to refer to a page or user control (by file name or virtual path) to programatically refer to members of the page or control. @Reference主要用于引用页面或用户控件(通过文件名或虚拟路径)以编程方式引用页面或控件的成员

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

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

@Register is the more commonly used directive. @Register是更常用的指令。 You use this when you want to use a user control in your aspx or ascx page declaratively. 如果要以声明方式在aspx或ascx页面中使用用户控件,请使用此方法。 @Register associates the control with a specific prefix and you can then use it in your markup. @Register将控件与特定前缀相关联,然后您可以在标记中使用它。

@Reference only tells ASP.NET to compile the other control when your aspx or ascx page is compiled. @Reference只告诉ASP.NET在编译aspx或ascx页面时编译其他控件。 That makes sure it is available at run-time and can be added to your control hierarchy programmatically. 这可确保它在运行时可用,并可以编程方式添加到控件层次结构中。 This is less common since dynamically changing user controls at runtime is not comon. 这不太常见,因为在运行时动态更改用户控件不是comon。

Here's a good blog post about it. 这是一篇关于它的好博文。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM