How to unload usercontrol from within itself

Hi all

Maybe i want to do this the wrong way.
I have a page with a button and a panel.

I click the button and a user control is loaded using the LoadControl function. The user control is added to the panel.
The user control has a button, when i click this button from within the user control, I want to remove the user control from the panel.

ASPX contains
Button - which loads a control and adds it to the panel
Panel - the user control get added to this control

ASCX contains
button - when clicked, I want it to unload/remove the user control from the panel.

How can I do this?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason ScolaroCommented:

This article will be useful regarding what I'm about to say:

Basically you should declare an event in your User Control and when that button (within the UC) is pressed it fires this separate event, lets call it RemoveControl.  This event will bubble up to the Page where this allows the Page to handle the event.  In the event handler, simply do your User Control removal logic.

See the "Event Driven Communication" section of the article above.

Check that out and let me know what you think and whether you need any further help implementing it.

Good luck!
-- Jason

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ValimaiAuthor Commented:
Thank you Jason (scolja)

Here is the result with a test project that works great (for anyone else interested)
Add new webform, UC_EventBubble_Default.aspx (WEBFORM)
Add new user control UC_EventBubble_WebUserControl.ascx (USERCONTROL)

Add USERCONTROL as register tag
Add the following code:

        protected void Page_Load(object sender, EventArgs e)
            UC_EventBubble_WebUserControl uc = (UC_EventBubble_WebUserControl)LoadControl("UC_EventBubble_WebUserControl.ascx");
            uc.ID = "ucControl";
            uc.CloseClick += new CommandEventHandler(uc_CloseClick);

        private void uc_CloseClick(object sender, CommandEventArgs e)

Add button called butClose.
Add the following code:
        public event CommandEventHandler CloseClick;

        protected void Page_Load(object sender, EventArgs e)


        protected void butClose_Click(object sender, EventArgs e)
            if (CloseClick != null)
                CloseClick(this, null);

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.