We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to unload usercontrol from within itself

Valimai
Valimai asked
on
Medium Priority
776 Views
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?

thanks
Comment
Watch Question

Top Expert 2006
Commented:
Valimai,

This article will be useful regarding what I'm about to say:
http://www.codeproject.com/aspnet/Page_UserControl.asp

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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)

WEBFORM
------------
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);
            form1.Controls.Add(uc);
           
        }

        private void uc_CloseClick(object sender, CommandEventArgs e)
        {
            form1.Controls.Remove(form1.FindControl("ucControl"));
        }


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


Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.