Registering User Controls in the Web Configuration File

 

In Asp.Net we have the User Control functionality to place the common functionality. We already discussed about User Controls in Asp.Net. In this article we discuss about different ways to register the User Control in Asp.Net.

 

We can register the User Control in each page as shown below.

 

<%@ Register Src="~/UserControlExp.ascx" TagName="UControl" TagPrefix="UC" %>

 

If we register the control in particular page, that user control is available in only that page. If you want to make the user control available for all web pages, we have to register the user control in web.config as shown below.

 

 

<?xmlversion="1.0"?>

 

<!--

  For more information on how to configure your ASP.NET application, please visit

  http://go.microsoft.com/fwlink/?LinkId=169433

  -->

 

<configuration>

    <system.web>

        <compilationdebug="true"targetFramework="4.0" />

      <pages>

        <controls>

          <addtagName="UControl"tagPrefix="UC"src="~/UserControlExp.ascx"/>

        </controls>

      </pages>

    </system.web> 

</configuration>

 

As show above we have to register the User Control in Web.Config under controls tag in pages tag. As shown above to register the user control we have three important properties. First property is src, we have to provide the User Control path. For tagName we can provide our own name for user control and for tagPrefix provide prefix name.

 

As we have tagName as UControl and tagPrefix as UC, we have to use the User Control  as shown below.

 

<UC:UControl ID="usercontrol1" runat="server" />