ASP.Net User Controls have the .ascx file extension and contains HTML, ASP.Net and C# code. User Controls are reusable controls and can be inserted into an ASP.Net page.
User Controls can be inserted into an ASP.Net page at design time by simply dragging the User Control onto the design view of that page in the Visual Studio.Net.
Other way to add the User Control to an ASP.Net page is by adding the following ASP.Net code in the page's HTML. First you need to register the User Control which can be done by adding the following code snippet just after the "Page" directive tag of the page:
<%@ Register src="~/usercontrols/TestUserControl.ascx" tagname="TestUserControl" tagprefix="UserControl" %>
Now add the following code snippet to insert the User Cotrol in the page:
<UserControl:TestUserControl id="TestUserControl1" runat="server"></UserControl:TestUserControl>
Both of the above approaches add User Control at design time only. But sometime you may need to add the User Control at run time. Don't worry; you can also add a User Control programmatically using the following C# code:
// Create instance of the UserControl TestUserControl
TestUserControl TestUserControl1 = LoadControl("~/usercontrols/TestUserControl.ascx") as TestUserControl;
//Add the TestUserControl to the respective Placeholder added in the ASP.Net Page
Placeholder1.Controls.Add(ucSimpleControl);