Labels

Wednesday, July 4, 2007

How to Register User Controls and Custom Controls in Web.config

Problem:

In previous versions of ASP.NET , to use the custom server controls and user controls on a page, <%@ Register %> directive is used at the top of pages like so:

<%@ Register TagPrefix="ucl" TagName="header" Src="Controls/Header.ascx" %>
<%@ Register TagPrefix="ucl" TagName="footer" Src="Controls/Footer.ascx" %>
<%@ Register TagPrefix="ControlVendor" Assembly="ControlVendor" %>

Once registered these controls can be used anywhere on the page by using the tagprefix and tagnames configured.


<html>
<body>
    <form id="form1" runat="server">
        <ucl:header ID="MyHeader" runat="server" />
    </form>
</body>
</html>

This works fine, but can be a pain to manage when you want to have controls used across lots of pages within your site (especially if you ever move your .ascx files and need to update all of the registration declarations.

Solution:

ASP.NET 2.0 makes control declarations much cleaner and easier to manage. Instead of duplicating them on all your pages, just declare them once within the new pages->controls section with the web.config file of your application:

<?xml version="1.0"?>

<configuration>

  <system.web>
    
    <pages>
      <controls>
        <add tagPrefix="ucl" src="~/Controls/Header.ascx" tagName="header"/>
        <add tagPrefix="ucl" src="~/Controls/Footer.ascx" tagName="footer"/>
        <add tagPrefix="ControlVendor" assembly="ControlVendorAssembly"/>
      </controls>
    </pages>

  </system.web>

</configuration>

Once registered the controls within the web.config file, can then be used similar to above.

<html>
<body>
    <form id="form1" runat="server">
        <ucl:header ID="MyHeader" runat="server" />
    </form>
</body>
</html>

Hope this helps,

 

 

Thanks & Regards,

Arun Manglick

SMTS || Microsoft Technology Practice || Bridgestone - Tyre Link || Persistent Systems || 3023-6258

 

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

No comments:

Post a Comment