我需要从(手动订阅)在asp.net事件退订? [英] Do I need to unsubscribe from (manually subscribed to) events in asp.net?

查看:231
本文介绍了我需要从(手动订阅)在asp.net事件退订?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行相同的最佳practis规则有关订阅/退订事件在asp.net应用?

Do the same best practis rules regarding subscribing/unsubscribing to events apply in asp.net?

我知道这似乎是一个愚蠢的问题,但是当我仔细想想,我从来没有真正看到任何code,人们首先订阅事件在页面上,然后在Web请求取消以后。

I know it might seem like a silly question, but when I think about it, I have never really seen any code where people first subscribe to an event on a page and then unsubscribe later on in the web request.

实施例1:
在一个页面,在Page_Load方法中,我订阅在一个ListView一个更新的事件。我应该从该事件取消以后,比如在在preRenderComplete方法?

Example 1: On a page, in the Page_Load method, I subscribe to a updating event on a ListView. Should I unsubscribe from that event later on, for example in the OnPreRenderComplete method?

实施例2:
在被动视图拍打,一个视图(页控制/用户控件)将提高时,它需要presenter做任何一个事件。因此,presenter需要订阅的查看事件,但它也需要从事件再次退订?

Example 2: In the passive view patter, a view (Page control/Usercontrol) will raise an event whenever it needs the presenter to do anything. So the presenter needs to subscribe to events on the view, but do it also need to unsubscribe from the events again?

最好的问候,埃伊尔。

推荐答案

该页面实例及其所有组件都将走出去的范围请求完成时,例如他们有资格获得GC。所以,你的ListView将走出去的范围与它的页/用户控件一起。你不需要退订(除非您订阅属于某种单身的那个幸存每个请求的事件,并使用页面作为事件处理程序的方法之一,例如)。

The page instance and all of its components will "go out of scope" when request completes, e.g. they become eligible for GC. So your ListView will go out of scope along with the Page/user controls on it. You don't need to unsubscribe (unless you subscribe to an event that belongs to some kind of singleton that survives every request and use one of the methods of the page as the event handler, for example).

(因为这presenter与一个页面单独使用,之后超出范围只要再)同样的事情有效期为presenter。

The same thing is valid for the presenter (again as long as this presenter is used solely with one page and goes out of scope after that).

这篇关于我需要从(手动订阅)在asp.net事件退订?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆