ASP.NET @Register vs. @Reference

contactmatt picture contactmatt · Jan 30, 2011 · Viewed 22k times · Source

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

Answer

Mark Cidade picture Mark Cidade · Jan 30, 2011

@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
%>