Thursday, March 31, 2011

How can I get my user control to know when the parent hears the user controls OnHide event?

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?

From stackoverflow
  • 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.

    Carter : Thanks for the input. It seems like the OnHide is exactly what I want, but that is an event that the control itself throws so code for it would go in my pages codebehind (which is what I don't want). If I had access to the close button I could achieve what I want by hooking into it's onclick.
  • You can just put an event into your custom control that fires when the user hits a button to close it. when this happens you can handle it via the page that has the control on it and make any changes that you need to the rest of the page or the app. If the control isn't a custom control where you can access the code-behind then you might want to just test for it's visibility, that may work in some cases.

    Carter : Well, I have an event that fires when the close is clicked, onhide. The problem is I need to check for the window being hidden from the code behind for the control, not the page containing the control.
    Middletone : if you use me.visible from the code behind this may work. A control can see it's own status this way. I'm not sure if it works if the parent control is hidden, I'll have to test this for you.
  • In the custom control, there are several methods you can override - particularly:

    protected override void OnUnload(EventArgs e) {
    
        // Insert custom code here
    
        base.OnUnload(e);
    }
    
    Carter : I appreciate the input. I at first thought about overriding the unload event raiser, but the problem is that it will still raise at times when I don't want it to. Also, if I changed this it would change for all of the other user controls I use in the app.
    Fritz H : You could then try deriving a custom UserControl from the original one with OnUnload overridden to perform the tasks you want, in additional with perhaps setting or checking flags elsewhere to see if it should perform the tasks?
  • I'm not sure if I understand you correctly, you have a usercontrol, that contains a "close" button. When this button is clicked, you hide the control, and want something beeing done?

    If you wan't it inside the control you could add it to the buttons click event, just after the hiding (as the button should effektively hide the control right?).

    If you wan't to handle your actions outside the usercontrol on the page itself you can simply add a custom event (like Middletone suggested). Here's how it would work, in codebehind:

    /// <summary>
    /// This is the Event Delegate
    /// </summary>
    public delegate void MyEventHandler(object sender, EventArgs e);
    
    /// <summary>
    /// This event is thrown when <...>
    /// </summary>
    public event MyEventHandler NewMyEvent;
    
    /// <summary>
    /// Internal event that triggers the external events
    /// </summary>
    /// <param name="e"></param>
    private void OnNewMyEvent(EventArgs e)
    {
        if (NewMyEvent != null)
            NewMyEvent(this, e);
    }
    

    Now, whenever you want the Event to be trown (e.g. when the handling of the close button is finished) Just call

    NewMyEvent(new EventArgs());
    

    and you're all set. In that case you would call that after setting the hide command.

    Hope it helps :)

    Carter : unfortunately I do not have access to the close button. This button comes from an inherited control. If you wouldn't mind, maybe take a look at my most recent update and give some insight if you have any.

0 comments:

Post a Comment