C# – How to get the user control to know when the parent hears the user controls OnHide event

asp.netcuser-controls

I have an asp.net page with a task bar on the side. Clicking a task opens up a user control for that task with some controls and a close button.

How do I run some C# code when that user control is hidden by clicking the close button?

Much of the stuff on the control is surrounded by an update panel and I'll just say right now that I have to use it. Anyway, I would just use the page_unload event but this is called in many other instances than the one single instance I want it to be called in (window is closing) partly because of the updatepanel.

The close button is not something I've implemented, it's just part of the framework being used for these controls, so it doesn't have any code behind.

All I really want to accomplish is to have some C# code run only when the control is closed, which in effect just hides it. Is there something analogous to Page_Closing/Hiding? I can't seem to find it if there is.

Is there any way to achieve this?

EDIT: I should note that I did find the OnHide event for the control, but the code behind for this logic should really be in the controls, not the pages. Mainly because what needs to happen is when the control is hidden, set some text in the control so when it is loaded up later it will display the correct info.

EDIT: Thanks for all the insights. I'm still not really able to solve the problem, what teedyay suggested…

"If you don't want to handle that code on the page, make your own version of the control, inheriting from the original. You can handle the OnHide event in a more self-contained way in there."

Is probably the closest to what I need. I'm wondering if it is at all possible to bubble events from the parent to the user controls? Logically in my head it seems like it wouldn't be possible and I haven't found any info on it but I thought I'd ask.

Update:
I think stepping back and stating a little more clearly what I want to accomplish may clear up some confusion.

I have a user control that is a floating panel that hovers over my main page. It inherits from a control that I do not have the code for. This control generates URL's (upon user request) and displays them within the control it also shows a history of the last 3 URLS generated.

When the page recognizes the controls OnHide event being fired, I need the code within the user control to disable some buttons and store the url in a history queue. How can I get my user control to know when the parent hears an OnHide event?

Also, am I thinking about this wrong, if so is there a smarter way to do this?

Best Answer

The Page_Unload event will be no good to you: this happens at the end of the page's lifecycle on the server - i.e. before the page content has even been sent to the client.

Remember that the web is stateless, so the server has no recollection of what page it has served up to whom, unless you go out of your way to remember.

If the user control you're using has a close button and you want to hook up to that being clicked then you can only really use whatever interface the developer of that control has given you. In this case it sounds like the OnHide event is your way in.

If you don't want to handle that code on the page, make your own version of the control, inheriting from the original. You can handle the OnHide event in a more self-contained way in there.