?
Solved

How to unload usercontrol from within itself

Posted on 2006-03-21
2
Medium Priority
?
699 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
0
Comment
Question by:Valimai
2 Comments
 
LVL 22

Accepted Solution

by:
Jason Scolaro earned 1000 total points
ID: 16256708
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
0
 
LVL 1

Author Comment

by:Valimai
ID: 16286571
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);
            }
        }


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

862 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