Skip to content
Loading
  • Satyapriya Nayak
    0
    Hi Rajkamal, To create an instance of a user control programmatically In the user control, be sure that the @ Control directive contains a ClassName attribute that assigns a class to the user control. The following example sets the ClassName attribute to strongly type a user control. Copy <%@ Control className="MyUserControl" %> In the page where you want to work with the user control, create a reference to the user control with the @ Reference directive. When you create the user control programmatically, the strong type for your user control is available to the ASP.NET Web page only after you have created a reference to it. For example, the following code creates a reference to a user control created in the MyUserControl.ascx file. Copy <%@ Reference Control="MyUserControl.ascx" %> Create an instance variable for the user control, using the control's class name. The class will be part of the ASP namespace. For example, if you want to create an instance of the user control declared as class Spinner, you use syntax such as the following: VB Copy Protected Spinner1 As ASP.Spinner C# Copy Protected ASP.Spinner Spinner1; Create an instance of the user control in code by calling the LoadControl method. Assign property values as necessary, and then add the control to the ControlCollection collection of a container on the page, such as a PlaceHolder control. The following example shows an ASP.NET Web page that loads a user control programmatically. The page includes an @ Reference directive to specify the control's file. The LoadControl method reads the file and instantiates it as a control that can be added to the page. C# VB Copy <%@ Page Language="C#" %> <%@ Reference Control="~/Controls/Spinner.ascx" %> Load User Control Programmatically



    Also Please refer the below links, http://msdn.microsoft.com/en-us/library/c0az2h86.aspx, http://www.c-sharpcorner.com/uploadfile/jayendra/how-to-create-user-control-in-Asp-Net/ Thanks
    0