How to unload usercontrol from within itself

Posted on 2006-03-21
Last Modified: 2012-08-14
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?

Question by:Valimai
    LVL 22

    Accepted Solution


    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
    LVL 1

    Author Comment

    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);


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now