@ Register

Associates aliases with namespaces and class names for concise notation in custom server control syntax.

<%@ Register tagprefix="tagprefix" Namespace="namespace" Assembly="assembly" %>
<%@ Register tagprefix="tagprefix" Tagname="tagname" Src="pathname" %>

Attributes

  • tagprefix
    An alias to associate with a namespace.

  • tagname
    An alias to associate with a class.

  • Namespace
    The namespace to associate with tagprefix.

  • Src
    The location (relative or absolute) of the declarative user control file to associate with the tagprefix:tagname pair.

  • Assembly
    The assembly in which the namespace that you are associating with tagprefix resides.

    Note   The assembly name does not include a file name extension.

Remarks

Including the @ Register directive in a page or user control allows you to lay out custom server controls or user controls using declarative custom server control syntax.

Use the @ Register directive in the following situations.

  • To declaratively add a custom ASP.NET server control to a page or user control.
  • To add a declarative user control to a page or user control.

For declarative user controls, use the tagname, tagprefix, and src attributes. The first two are always used together as a colon-separated pair (tagprefix:tagname) when you declare the control in the page. The src attribute value can be either a relative or absolute path to the user control source file from your application's root directory. For ease of use, it is recommended you use a relative path. For example, assume you store all of your application's user control files in a \usercontrol directory that is a sub-directory of your application root. To include the user control found in a usercontrol1.ascx file, include the following in the @ Register directive:

Src="~\usercontrol\usercontrol1.ascx" 

The tilde (~) character represents the root directory of the application.

Note   If your user control is in the same directory as the page that contains it, the src attribute value should be the name and extension of the .ascx file.

When including custom server controls that you have compiled into a .dll for use with your application, use the tagprefix with the Assembly and Namespace attributes. If you do not include the Namespace attribute, or if you assign an empty string to it, a parser error will occur.

CAUTION   When you develop a custom server control, you must include it in a namespace. If you do not, it will not be accessible from an ASP.NET page. For more information about developing custom ASP.NET server controls, see Developing a Simple ASP.NET Server Control.

Example

The following code fragment uses @ Register directives to declare tagprefix and tagname aliases for a server control and a user control. The first directive declares the MyTag alias as a tag prefix for all controls residing in the MyCompany:MyNameSpace namespace. The second directive declares Acme:AdRotator as a tagprefix:tagname pair for the user control in the file Adrotator.acscx. The aliases are then used in custom server-control syntax within the form to insert an instance of each server control.

<%@ Register Tagprefix="MyTag" Namespace="MyCompany:MyNameSpace" Assembly="MyAssembly" %>
<%@ Register Tagprefix="Acme" Tagname="AdRotator" Src="AdRotator.ascx" %>
<HTML>
 <body>
   <form runat="server">
      <MyTag:MyControl id="Control1" runat="server" /><BR>
      <Acme:AdRotator file="myads.xml" runat="server" />
   </form>
 </body>
</HTML>

See Also

ASP.NET Web Forms Syntax | Web Forms User Controls | Directive Syntax