ASP.NET @Register vs. @Reference

被刻印的时光 ゝ 提交于 2019-12-03 10:37:13

问题


I'm working with referencing user controls on my ASPX page and I'm wondering what the difference is between these two page directives.

@Reference @Register


回答1:


@Register is the more commonly used directive. You use this when you want to use a user control in your aspx or ascx page declaratively. @Register associates the control with a specific prefix and you can then use it in your markup.

@Reference only tells ASP.NET to compile the other control when your aspx or ascx page is compiled. 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.

Here's a good blog post about it.

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




回答2:


@Register is primarily used for registering tag prefixes to declaratively use controls within a page.

<%@ 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 Control="MyControl.ascx" %>

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


来源:https://stackoverflow.com/questions/4840985/asp-net-register-vs-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!