Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 612393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:53:19+00:00 2026-05-13T17:53:19+00:00

I have hard time to modify a page that had a Custom User Control

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:53:19+00:00Added an answer on May 13, 2026 at 5:53 pm

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a buddy that's having a hard time with the language barrier. I
I have a hard time believing that there isn't an answer here but it
I always seem to have a hard time starting a new Firefox extension. Can
This seems like a pretty softball question, but I always have a hard time
I have a binary field in my database that is hard to describe in
I am having a really hard time getting this marshalling down. I have umanaged
I am trying to study the jquery class, but I have a hard time
First off, I am using Windows XP. I have multiple hard drives and it
I have a RHEL 5 system with a fresh new hard drive I just
We have SQL Server 2005 running on Win2003/64, and we push it hard. We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.