Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Consuming Custom Controls in ASP.NET

RSS
Modified on Fri, Aug 03, 2012, 8:46 AM by Administrator Categorized as ASP·NET Web Forms, Class Library
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>

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2017, Patrick Jasinski.