23

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.

5
  • 1
    Can you post the code that you are using to load the user control and the? Are loading the control via ajax or on a full page load? Commented Feb 16, 2010 at 19:27
  • 1
    Page.LoadControl() works fine for me. During handling of which event (Page.Init, Page.Load, etc) do you load it? Commented Feb 16, 2010 at 19:28
  • 1
    Page load and Page PreRender does the same behavior of crashing on NULL object that has been declared in the ASCX user control file Commented Feb 16, 2010 at 19:32
  • 1
    I tested your code and I also get null but when I use the code in my answer it works fine. Commented Feb 16, 2010 at 19:50
  • 1
    I will try to LoadControl with the path instead of the type like your did Commented Feb 16, 2010 at 19:54

1 Answer 1

45

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;
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Your solution doesn't raise the error that I had but I cannot see the controls. If I try to simply add new LiteralControl("TEST"); I do see the "Test" in the source code but not the control markup...
Does it works if you have some Text directly into the ASCX? I'll try to produce a small code to create the problem I have by this time.
It works with a simple test but with the big component it doesn't load anywhere (but it doesn't crash) very weird...
I had a Visible False somewhere that wasn't showing it. I use your solution, it works fine +1 and the answer!
Why is Page_Init() used as opposed to Page_Load()?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.