我应该在什么时候调用 ASP.NET 事件的基本方法?
在 ASP.NET 中,如果我重写页面生命周期事件,我应该在工作之前还是之后调用其基本方法? 这还重要吗?
protected override void OnPreRender(EventArgs e)
{
// My code goes here
base.OnPreRender(e);
// Or here
}
In ASP.NET, if I override a page lifecycle event, should I call its base method before or after doing my work? Does it even matter?
protected override void OnPreRender(EventArgs e)
{
// My code goes here
base.OnPreRender(e);
// Or here
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您无论如何都要调用页面基本方法,您可以使用
而不是
If you're going to call the page base methods anyway, you can use
rather than
我认为原则上称呼他们是个好主意。 在您当前使用的框架版本中,基类方法中可能没有代码,但谁知道未来的版本。 此外,关注点分离将规定您编写的从 Page 派生的代码不会假定 Page 类不执行任何操作,而是在其 OnPreRender 方法中引发 PreRender 事件。
I think it's a good idea to call them just on principle. It may be true that in the framework version you're currently using there's no code in the base class methods, but who knows about future versions. Also, separation of concerns would dictate that code you write that derives from Page not assume the Page class doesn't do anything but raise the PreRender event in its OnPreRender method.
没有单一的规则。 我可以给你举个例子。 我的 ASP.net web 应用程序使用由母版页打开的 NHibernate 事务,并在页面结束时由它提交/回滚。
好吧,我必须在 OnInit 方法中尽早初始化事务(Master 没有像 Page 那样的 OnPreInit),否则用户控件在 Page.Load 之前无法访问事务。
同样的规则适用于提交。 控件可能想要在其生命周期的最后阶段更新对象,那么我必须在 Unload 方法中甚至在处理程序中尽可能晚地关闭事务!
所以...就我而言...
我建议您遵循一条一般规则:如果您的页面设计契约涉及在特定事件范围(即加载之后和 PreRender 之前)之间创建和销毁控件使用的资源,请初始化在事件触发之前尽可能晚地资源,并在最终事件触发之后尽早销毁它
There is no single rule. I can provide you an example. My ASP.net webapp uses a NHibernate transaction opened by the master page and commited/roll-backed by it when the page ends.
Well, I MUST initialize the transaction as early as possible in the OnInit method (Master has no OnPreInit like Page), otherwise user controls cannot access the transaction until Page.Load.
The same rule applies for commit. Controls may want to update objects in the last phases of their life-cycles, then I must close the transaction as latest as possible in the Unload method, or even in the disposer!
So... in my case...
I would suggest you a general rule: if your page's design contract involves creating and destroying resources to be used by controls between a certain event range (ie. after Load and before PreRender), init the resource as late as possible before the event is fired, and destroy it as early as possible after the final event is fired
是的,你应该关心。 假设您需要在所有这些页面中插入一个新的基类。 对我来说,直接调用基本方法比稍后进行大量重构更容易。
话又说回来,也许你不需要这样做。
编辑
根据对问题的编辑,这里有更多信息:
是的,你应该关心。 有时您希望基类方法在您的方法之前触发(对于构造函数),有时您希望它在您的方法之后触发(析构函数)。
这可能意味着代码访问属性或对象时是否可用之间的差异。
Yes you should care. Let's say for a moment that you need to insert a new base class in all of those pages. To me it's easier to just go ahead and call the base methods than have to do a lot of refactoring later.
Then again, maybe you don't need to do that.
EDIT
Based on the edit to the question here's some more info:
Yes, you should care. Sometimes you want the base classes method to fire before yours (in case of constructors), and sometimes you want it to fire after yours (destructors).
It may mean the difference between whether a property or object is available or not at the time your code gets to it.
asp.net 事件模型中的“OnEvent”方法仅包装实际的事件调用(在本例中为“PreRender”事件)。 因此,您唯一需要决定的是“我需要在工作之前还是之后调用该活动”?
The "OnEvent" methods in the asp.net event model merely wrap the actual event calls (in this case, the "PreRender" event). So the only thing you need to decide is "do I need to call the event before or after I do my work"?
答案是,这取决于代码在做什么,是否应该放在前面或后面。
正如另一个人所说,如果它是构造函数的东西,它应该放在前面。 析构函数应该紧随其后。 举一个更具体的例子,如果您有处理页面并加载内容、填充下拉列表和填充标签等的代码,那么您会希望在任何查看预填充内容并确定可见性或与页面上的数据有关的业务规则逻辑。
The answer is, it depends on what the code is doing as to if it should go before or after.
As another said, if it is constructor stuff it should go before. Destructor should go after. To give a more concrete example, if you have code that processes the page and loads content, fills drop downs, and fills labels and such then you would want that to happen before any code that looks at what is pre-populated and determines visibility or business rule logic that has to do with the data on the page.