Wednesday, September 15, 2010

Register User Controls and Custom Controls in Web.config

One mistake most of developers (including me before writing this article) do. When we use user controls or custom controls, we use them by registering on the page where we are using that particular control (<%@ Register %> directives to the top of pages).

Instead of duplicating code all where we can do same with much easier and cleaner approach, by declaring them in web.config file.

<configuration>
  <system.web>
    <pages>
      <controls>
         <add tagPrefix="ucl" src="~/MyControls/uclMyControl.ascx" tagName="MyControl"/>
      </controls>
    </pages>
  </system.web>
</configuration>

Once you register the controls within the web.config file, you can then just use the controls on any page,

<ucl:MyControl id="mcLogin" runat="server"> </ucl:MyControl>

No comments:

Post a Comment