Creating an ASP.NET Web User Control is as simple as creating an ASP.NET Page using  Visual Web Developer.

Here  are the steps:

1. Open up an existing Website to which you want to add the user control.

2. Right Click on the Website context menu and click on “Add New Item … ”. This will bring up the Add New Item dialog box with a list of Visual Studio installed templates. Select “Web User Control” as shown in the picture below.

Web User Control

3. Click Add. This will add the user control to your website. You can open the User Control in Design View and start adding controls to it from the tool box.

4. The ASP.NET user control looks very similar to a ASP.NET Web Page, except that it is stored with an .ascx extension and it has a @Control directive rather than a @Page directive. Also, note that the User control does not contain html, body and form elements. The control directive looks like this:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>

 5.  Now, registering this Web User control to your web page is as simple as dragging and 
  dropping the User Control from Solution Explorer to the required web page in design view
  mode. At this point Visual Studio will automatically register the User Control in the web page
  by adding the @Register directive in your web page as shown below.
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1"%>

The tagname attribute is the name of the user control and tagprefix is used to determine a

unique namespace for the user control. Src attribute determines the path to the user control.

 

6. Visual Studio also adds the user control to the page by adding the following line of code.

 

<uc1:WebUserControl ID="WebUserControl1" runat="server" />

Note that the TagName along with the TagPrefix is used to identify the Control.

Reshmi Mangalore

SDET, Web Development Tools