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 6703889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:13:30+00:00 2026-05-26T07:13:30+00:00

Trying to dynamically add a user control that dynamically generates content. The User Control

  • 0

Trying to dynamically add a user control that dynamically generates content. The User Control cannot get a handle on the panel to put controls in.

First I have a page (test.aspx):

<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    </form>
</body>
</html>

code behind:

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TestUserControl uc = new TestUserControl();
        Panel1.Controls.Add(uc);
        //this is where the error happens:
        uc.Fill();
    }
}

and then here is the user control:

    <asp:Panel ID="pnlTest" runat="server" >
    </asp:Panel>

and the code behind:

public partial class TestUserControl: System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public void Fill()
    {
        Label lbl = new Label();
        lbl.Text = "test";
        //This is where pnlTest is null and I get "Object Reference Not Found..."
        pnlTest.Controls.Add(lbl);
    }
}

So… It seems like the point at which I’m calling Fill() is before the user control has been rendered, therefore pnlTest has not been instantiated. But, I’m not sure where to call uc.Fill() from test.aspx … I tried Page_PreRenderComplete, but that didn’t work either…

(if you see a name mis-match.. that’s probably not the error… the names have been changed to protect the innocent)

  • 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-26T07:13:30+00:00Added an answer on May 26, 2026 at 7:13 am

    hurray, I can answer my own question.
    I changed test.aspx.cs to this:

    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            TestUserControl uc = (TestUserControl)Page.LoadControl("~/UserControls/TestUserControl.ascx");
            Panel1.Controls.Add(uc);
            uc.Fill();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a make generic select control that I can dynamically add elements
I am trying to add controls to a UserControl dynamically (programatically). I get a
I am trying to add dynamically a user control I created to an aspx
I have a user control that loads other user controls dynamically. The issue is
My problem is I'm trying to add user controls to a page dynamically after
Question I am trying to dynamically get the default for a type that is
I'm trying to add rows dynamically to a System.Web.UI.WebControls.Table control in ASP.NET. I found
I'm trying to create a web page that will display an appropriate user control
I am trying to swap out user controls dynamically. How can I 'hide' controls
I'm trying to dynamically populate a dropdown when a user is trying to add

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.