Reputation: 2075
I am subscribing to an event in a masterpage from one of the pages in the page_load event i.e. I have the code
this.Master.Event1 += new Event1EventHandler(Master_Event1);
in page_load event.
If I dont unsubscribe from the event can this cause a memory leak? What would be an appropriate way to unsubscribe? Should I do it in the page_unload event? That will handle the user action of exit from the page, but what would be the correct way to handle it if the user closes the browser? Session_end in global.asax?
Thanks
Upvotes: 1
Views: 555
Reputation: 5975
Unlike Windows Forms or WPF, ASP.NET is stateless (as is the rest of all web server solutions). In short, that means that when a GET request comes in, the web server (IIS) handles the request and generates a html page based on the aspx page that you built (with or w/o a Master Page).
So, when you hook an event handler to an event fired from the Master Page, you can only handle it in the same request, in the content page or maybe any user controls that you load into that content page.
Why would you fire any event in your Master page? If you tell us more about which functionality you're looking for, we might be able to provide an alternative - maybe even better - solution.
Read more about ASP.NET on the following MSDN pages, or simply Google yourself to knowlegde :)
ASP.NET Page Life Cycle Overview
Events in ASP.NET Master and Content Pages
Upvotes: 0
Reputation: 65126
Event handler lists are in essence weak references, so that will not cause a memory leak. You're not explicitly removing all your Click
and whatever handlers either, are you?
Also, the Unload
event has nothing to do with the user closing the browser window. To me, it sounds like you've misunderstood some fundamentals of the web and/or ASP.NET.
Upvotes: 1