Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Custom user control to add dynamically

Tags:

I have hard time to modify a page that had a Custom User Control directly to the ASPX page and now require to have it dynamically loaded when needed only. The User Control does have html and other controls via the ASCX file and has code in the code-behind.

I have read multiple page and have found that I cannot instantiate directly the User Control but should use the Page.LoadControl(...). The problem is not the compiling but when the page load the control it happen that all controls inside the ASCX are null and then crash.

How can I use a User Control that has code in the ASCX and in the code-behind dynamically?

Edit:

Example of what I am doing in (PageLoad or PagePreRender or PagePreInit)

      Control c = LoadControl(typeof(MyControl), null);       myControl= (MyControl)c;       myControl.ID = "123";       myControl.Visible = false;       Controls.Add(myControl); 

MyControl does have for example <div id="whatever" runat="server">... and inside the MyControl it set the visibility to True or False... but when it does that, now it crash because the "whatever" div is NULL.

like image 835
Patrick Desjardins Avatar asked Feb 16 '10 19:02

Patrick Desjardins


People also ask

How user control are loaded dynamically?

The user control dynamically loaded at runtime is strategically placed in a table cell in the ItemTemplate . This loading takes place in the method that handles the ItemDataBound event for each row of the Repeater .

What is user control and custom control in ASP.NET with example?

User controls are custom, reusable controls, and they use the same techniques that are employed by HTML and Web server controls. They offer an easy way to partition and reuse common user interfaces across ASP.NET Web applications. They use the same Web Forms programming model on which a Web Forms page works.

How is a ASP.NET presentation page associated with its code behind?

The code-behind feature of ASP.NET enables you to divide an ASP.NET page into two files - one consisting of the presentation data, and the second, which is also called the code-behind file, consisting of all the business logic.


1 Answers

What I have done is use the Page.LoadControl method in the Page_Init to add the custom user control to a place holder on the page.

protected void Page_Init(object sender, EventArgs e) {     //MyControl is the Custom User Control with a code behind file     MyControl myControl = (MyControl)Page.LoadControl("~/MyControl.ascx");      //UserControlHolder is a place holder on the aspx page     // where I want to load the user control to     UserControlHolder.Controls.Add(myControl); } 

This works fine for me.

Here is the code for the dynamically loaded user control:

MyControl.ascx.cs

public partial class MyControl : System.Web.UI.UserControl {     protected void Page_Init(object sender, EventArgs e)     {         LiteralControl lit = new LiteralControl("Test Literal Control");         Page.Controls.Add(lit);     }      protected void Page_Load(object sender, EventArgs e)     {         whatever.Visible = true;          if (IsPostBack)         {             whatever.Visible = false;         }     } } 
like image 113
orandov Avatar answered Sep 28 '22 03:09

orandov