Dear friends,
How to add User controls in 3.5 give one example
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Apr 16, 2012, 12:55 AM EDT
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.
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.
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:
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#" %> "~/Controls/Spinner.ascx" %> "server"> private ASP.Spinner Spinner1 protected void Page_Load(object sender, EventArgs e) { Spinner1 = (ASP.Spinner)LoadControl("~/Controls/Spinner.ascx"); } protected void Button1_Click(object sender, EventArgs e) { PlaceHolder1.Controls.Add(Spinner1); } "Head1" runat="server"> Load User Control Programmatically "form1" runat="server"> "server" ID="PlaceHolder1" /> "Button1" runat="server" Text="Click to Add User Control" OnClick="Button1_Click" /> "Label1" runat="server" Text="">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
SenthilkumarRank #387 · 6.5K reputationPosted Apr 16, 2012, 7:52 PM EDT
The web user control is a separate page and it will be used to create the partial of page in the web pages. It can't run independentely.
It has the extension name of (.ascx) and can be add in the "Add new Item".
As you can see in the web page, the header, footer or custom page control can be created in the web user control and can be added to the page using the register directives.
It has to be added for every application which want to use the web user control and once registered in the web page can be create the multiple instances as well.
Please follow this link for steps.
http://msdn.microsoft.com/en-us/library/26db8ysc(v=vs.85).aspx
http://asp.net-tutorials.com/user-controls/using/