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

  • SEARCH
  • Home
  • 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 6840013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:47:19+00:00 2026-05-26T23:47:19+00:00

I have a webpage containing a panel and a button. When you click the

  • 0

I have a webpage containing a panel and a button. When you click the button, I want to add a new instance of a UserControl to the panel.

The panel.Controls.Add method specifies that it adds an item to a collection.

I can do this using a local variable to store the number of instances of the control. Then when one it added it loops this many times creating new controls.

private void AddUCToUI(int counter)
    {
        for (int i = 0; i < counter; i++)
        {
            MyControl ctrlMyControl = (MyControl)LoadControl("MyControl.ascx");
            ctrlMyControl.SetID(i);
            myPanel.Controls.Add(ctrlMyControl);
        }
    }

BUT, I don’t want to recreate all of the controls every time, as the user may have already entered data into them…

So all I want to do is create another control, and add it to the collection which already contains the first one.

private void AddUCToUI(int counter)
    {
        MyControl ctrlMyControl = (MyControl)LoadControl("MyControl.ascx");
        ctrlMyControl.SetID(counter);
        myPanel.Controls.Add(ctrlMyControl);
    }

This should create a new control, give it an ID, then add it to the collection. However it seems to add a control in the first instance, then overwrite this control in the collection when I attempt to add another. Why is this?

  • 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-26T23:47:20+00:00Added an answer on May 26, 2026 at 11:47 pm

    I have managed to solve this issue:

    Moving the code from onInit to Page_Load and using a session variable for counter solved the issue of incrementing it’s value. Session variables are not reset when a PostBack happens.

    Here is my solution:

    protected override void Page_Load(object sender, EventArgs e)
    {
        int counter;
    
        if (Session["counter"] == null)
        {
            counter = 0;
        }
        else
        {
            counter = (int)Session["counter"];
        }
    
        for (int i = 0; i < counter; i++)  
        {  
            MyControl ctrlMyControl = (MyControl)LoadControl("MyControl.ascx");  
            ctrlMyControl.ID = String.Format("ctrl_{0}", myPanel.Controls.Count); //count = 0
            myPanel.Controls.Add(ctrlMyControl); //count = 1
        }      
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webpage where I want the user to see a new image
What I have Is a centered div in my webpage containing a picture that
I have a standard webpage containing an UpdatePanel, and a button. Upon loading the
I have a webpage that pulls information from a database, converts it to .csv
I have a webpage that is taking way too long and need to optimize
I have a webpage on my site that displays a table, reloads the XML
I have a webpage that implements a set of tabs each showing different content.
I have a webpage that I use h1 tags multiple times within various DIVs
I have developed a webpage containing charts using HighCharts . It works fine on
Say I have a webpage containing a 200x200 image. At coordinates 50,50,150,150 (x1, y1,

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.