User Controls in .Net Framework

User controllers provide the easiest way to combine several controls onto a single control. 
if you need to use a functionality that is not provided by the framework then you can use user controls for create your own controls by using the existing controllers.

You can create User controls in a same way as you create Web Pages. You can reuse user controls in your web site several times. So user controls can reduce the complexity of your web pages and reduce the lines of coding in a single page.

User controllers also have the code behind pages as normal Web Pages. So you can write codes for controls inside user controls easily and encapsulate these codes from original web page.
In user controllers no needs to have a form tag in the page as normal web pages. So you can add your controllers to user controllers without adding any form tag.

Example for user control;

Creating User Controls

There have 2 ways to creating usercontrols.
First one is you can create a user control just like creating a web page in .net.
Add new item-> web->web User control

1.      Second one is you can convert your webpage to a user control
2.      Remove<html>,<body> and <form> start and end tags
3.      Change @Page directive to @Control
4.      Change file extension to .ascx
5.      In the @Control directive change inherit=”System.web.UI.Page” to inherit=”System.Web.UserControl”

Adding User Control to a Page
You can simply drag and drop user control to your webpage design area from solution explorer window.
Then your client code will looks like following,
<%@ Page Title="Home Page" Language="C#"  AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>

<form id="form1" runat="server">

<h2>
<asp:Label ID="Label1" runat="server" Text="Student Details"
        style="font-weight: 700; font-family: Cambria; color: #9900FF"></asp:Label>
</h2>
<p>
    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</p>
<h2>

</form>


uc1 is the user control’s tagname it is defined in the @Register directive.And src Property of the @Register define the actual path of the user control.


Usage of User Controls
 If your web site use same skeleton for several pages you can use user controls.it will reduce your effort and line of codes also.