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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:41:20+00:00 2026-05-16T15:41:20+00:00

I have a form with a panel on which I dynamically load multiple user

  • 0

I have a form with a panel on which I dynamically load multiple user controls. I handle events for each of these controls.

UserControl userControl1 = LoadControl("control.ascx") as UserControl;
userControl1.Event += new ControlEventHandler(userControl_Event);
this.Panel.Controls.Add(userControl1);

UserControl userControl2 = LoadControl("control.ascx") as UserControl;
userControl2.Event += new ControlEventHandler(userControl_Event);
this.Panel.Controls.Add(userControl2);

...

Now when I get rid of the controls on the Panel, I simply do a

this.Panel.Controls.Clear();

Does the Clear() function take care of getting rid of the events or should I be doing

foreach(Control control in this.Panel.Controls)
{
    UserControl userControl = control as UserControl;
    if(userControl != null)
    {
        userControl -= userControl_Event;
    }
}

before I Clear() the content of the Panel?

Basically, i’m looking for a way to load user controls dynamically and handle their events without creating a leak when I get rid of them.

Thanks!

EDIT:
Because my controls are created in the Page_Init event of the page (each time, as they are loaded dynamically), is it correct to say that their lifespan cannot be longer than the page’s lifespan?
From what I understand, the control doesn’t exist after a post back. A new one is created each time. Therefore, I shouldn’t have to unregister the event because the object it doesn’t even exist on the next page load. Is that correct?

  • 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-16T15:41:21+00:00Added an answer on May 16, 2026 at 3:41 pm

    The page will hold references to the dynamically instantiated controls even after the collection has been cleared, which will prevent the controls from being collected until the page is itself collected.

    In this particular situation this will work out fine because the life of the page is very short.

    However, if this were instead a windows forms app, then the memory would effectively be leaked until the form was released.

    In general it is a good idea to unsubscribe your events when you release the objects that the events go to, as this is the source of the vast majority of .net memory leaks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page with user controls getting dynamically added. Inside some of these
I have created user control container (panel actually) which dynamically adding and removing user
I have web form with a panel which has form elements in side it.
We have a panel which displays a form with an arbitrary number of JTextFields
I have a Panel which hosts a number of child controls in a grid
I have a form with a Panel which covers the entire form and serves
I have a parent form, which contains panels that calls different user control. For
I have a panel which displays Form objects. When the Close() method is called
In my form I have a listview and a panel which is used as
I have a form with a panel in which I want to display an

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.