ASP.NET 母版页事件
我从 page_load 事件中的一个页面订阅母版页中的一个事件,即我
this.Master.Event1 += new Event1EventHandler(Master_Event1);
在 page_load 事件中有代码。
如果我不取消订阅该事件会导致内存泄漏吗?取消订阅的适当方式是什么?我应该在 page_unload 事件中执行此操作吗?这将处理用户退出页面的操作,但是如果用户关闭浏览器,处理它的正确方法是什么? global.asax 中的 session_end?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事件处理程序列表本质上是弱引用,因此不会导致内存泄漏。您没有明确删除所有
Click
以及任何处理程序,是吗?此外,
Unload
事件与用户关闭浏览器窗口无关。对我来说,听起来您误解了 Web 和/或 ASP.NET 的一些基础知识。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.与 Windows 窗体或 WPF 不同,ASP.NET 是无状态的(与所有 Web 服务器解决方案的其余部分一样)。简而言之,这意味着当 GET 请求传入时,Web 服务器 (IIS) 会处理该请求并根据您构建的 aspx 页面(带或不带母版页)生成 html 页面。
因此,当您将事件处理程序挂钩到从母版页触发的事件时,您只能在同一请求、内容页或加载到该内容页中的任何用户控件中处理它。
为什么要在母版页中触发任何事件?如果您告诉我们更多有关您正在寻找的功能的信息,我们也许能够提供替代(甚至更好)的解决方案。
在以下 MSDN 页面上阅读有关 ASP.NET 的更多信息,或者直接通过 Google 自行了解知识:)
ASP.NET 页面生命周期概述
ASP.NET Master 和内容页面
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