Consuming Custom Controls in ASP.NET

To use a custom control on an ASPX page, you have one of two options. You can either register the control on a page-by-page basis, or register the control's namespace for the entire website.

Page-Based Registration

User Control (ASCX file)

Use use a custom control on just one page, you would include a Register tag at the top of the file, similar to the following. The Src attribute specifies the location of the custom control ASCX file, and the TagPrefix and TagName attributes specify what you would use in the ASPX markup. Note that this technique only works with ASCX files.

<%@ Register Src="~/UserControls/MyCustomControl.ascx" TagPrefix="abc" TagName="MyCustomControl" %>

Class-Based (VB or CS file)

Use use a custom control on just one page, you would include a Register tag at the top of the file, similar to the following.

%@ Register TagPrefix="abc" Namespace="AmericanBroomCompany.WebControls" Assembly="AmericanBroomCompany.WebControls" %>

Website-Based Registration

User Control (ASCX file)

In your web.config file, you would include an entry similar to the following. The tagPrefix and tagName attributes combine to specify what you use in the ASPX markup. Note that except for capitalization these attributes are the same as for the <%@ Register %> tag above.

<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="abc" tagName="PageSelector" src="~/UserControls/PageSelector.ascx"/>
        ...
      </controls>
    </pages>
  </system.web>
</configuration>

Class-Based (VB or CS file)

In your web.config file, you would include an entry similar to the following. The tagPrefix attribute specifies what you use in the ASPX markup; the namespace attribute specifies the namespace the control is declared in.

NOTE: If the class is hosted in a separate DLL, you will need to include the assembly attribute below and set a reference to that project.

<configuration>
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="abc" namespace="AmericanBroomCompany" assembly="ABC.Web.Controls" />
        ...
      </controls>
    </pages>
  </system.web>
</configuration>