UserControls in .net

User Control are the custom, reusable controls. User Controls offers a way to partition and reuse User Interface (UI) functionality across ASP.NET Applications.User controls will reduce the redundancy in the coding when you are developing applications. User Controls can encapsulate the logic inside it and reuse the control more times.

Advantages of User Control:

  1. Code Reuse
  2. Cache the output of the control independently using technique called fragment caching. This will improve performance if used appropriately.
  3. Can program against any properties declared in the control, just like ASP.NET web controls.

Disadvantages of User Control:

  1. User Controls can be instantiated in the pages that resides in the same web application. If you want to use across applications, you need to place same .ascx control
  2. Can’t hide code of user controls like server controls by compiling into an assembly.

Develop a small usercontrol 

Add a usercontrol in your project first.

For example we can develop a small registration page in that the fields like Address, City, State are created in a usercontrol.

Adding controls to a user control :-

<asp:TextBox ID=”txtAddress” runat=”server”></asp:TextBox>

<asp:TextBox ID=”txtCity” runat=”server”></asp:TextBox>

<asp:TextBox ID=”txtState” runat=”server”></asp:TextBox>

Adding User Controls to an WebForms Page:

In order to use a usercontrol we need to register the user control first as show below.

<%@ Register Src=”~/Usercontrols/WebUserControl.ascx” TagName=”UCSample” TagPrefix=”UC1″ %>

This directive registers the control so that it can be recognized when the page is processed. TagPrefix determines the unique namespace of the control, TagName is the name of the user control and Src is the path of the user control.

After registering a user control in your page, now we need to add the control in our page.

<UC1:UCSample ID=”ucSample” runat=”server” />

<asp:Button ID=”btnSave” runat=”server” Text=”GetValues” OnClick=”btnSaveClick” />

It’s easy to read the values of  controls(textbox etc) present in the page, but its required some effort to get the values from textbox/controls in usercontrol to my aspx page.

User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below

public string Address
get{return txtAddress.Text;}
set{txtAddress.Text = value;}

public string State
get { return txtState.Text; }
set { txtState.Text = value; }

public string City
get { return txtCity.Text; }
set { txtCity.Text = value; }

You can access the values of the textboxes in the control from an .aspx page by declaring public property in the .ascx page.

In .aspx page,you can access value of the Address textbox  using

protected void btnGetValuesClick(object sender, EventArgs e)
var address= ucSample.Address+ “, ” + ucSample.City+”, “+ucSample.State;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s