BeanSoftware Logo
 

 
ASP.NET Database Search Control
 
 

 
 Home
    Shop
    Advertise
    Write For Us
    Affiliate
    Newsletter
    Contact

How To Register Controls In Web.Config?

To register custom web controls or user controls developers often use Register directives on top of the page, like this:

<!-- Simple page directive -->
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!-- To register user control -->
<%@ Register TagName="poll" TagPrefix="uc" Src="~/UserControls/Poll.ascx" %>
<!-- To register web custom control -->
<%@ Register TagPrefix="cc1" Namespace="BeanSoftware.NET2" Assembly="WebSearchControl_v3_NET2-0" %>

 

After that, later in page you can access to this controls by using its tag prefix and tag or class name in markup code, like this:

<cc1:websearchcontrol id="search" runat="server" />

 

This is correct procedure, but if you need to insert same user controls or web custom controls in many pages, you can consider using of Controls section in web.config. To register controls in web.config, use code like this:

<pages>
 <controls>
  <!-- Register web user control -->
  <add  tagName="poll" tagPrefix="uc" src="~/UserControls/Poll.ascx" />
  <!-- Register web custom control -->
  <add  tagPrefix="cc1" namespace="BeanSoftware.NET2" assembly="WebSearchControl_v3_NET2-0" />
 </controls>
</pages>

 

Now you can use these controls in every page without registrations! If you need to use same user or custom controls on many pages, registering in every page looks a little bit hard coded and harder to maintain. By using web.config you have information on one place, which is easier to manipulate and change if needed.

Web.config registration remarks

Note that your web application will restart every time you register new control because you changed web.config file and even every time you update any registered control. Also, you can't place user controls and pages in the same folder, you need separate folder for user controls. If some user control uses other user control then it needs separate folder too. If user control is in same folder as page, you will receive an error. Also, you can't register controls in web.config if you use ASP.NET 1.1, since Controls section is introduced in ASP.NET 2.0.



Related articles:

1. Using ASP.NET With SQL Server

FAQ toolbar: Submit FAQ  |  Tell A Friend  |  Add to favorites  |  Feedback



Copyright © 2002-2008 Bean Software. All rights reserved.