创建 Web 控件时应该重写 OnLoad 还是实现 Page_Load
当您在 Visual Studio 中创建新的 Web 用户控件时,它默认添加 Page_Load 事件。 使用此事件而不是重写控件上的基本 OnLoad
事件有什么优势? 仅仅是 Page_Load
事件在 OnLoad
之前触发吗?
When you create a new web user control in visual studio it by default adds the Page_Load event. What is the advantage to using this rather than overriding the base OnLoad
event on the control? Is it just that the Page_Load
event fires before OnLoad
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
OnLoad
方法应该是引发Load
事件的地方。 我个人总是尝试处理该事件,除非我需要围绕引发事件进行额外的处理。我建议在正常情况下自行处理事件。
The
OnLoad
method should be the place where theLoad
event is raised. I personally always try to handle the event unless I need to do extra processing around raising the event.I recommend handling the event itself under normal circumstances.
我认为是一样的。
恕我直言,通过事件,您可以拥有更多的灵活性,因为您的事件可以有多个侦听器!
i think it's the same.
IMHO, With Events, you have a bit of more flexibility, because you can happen more than one listener to your event!
我认为这两种方法存在一个潜在的显着差异。
我指的是控制执行顺序的能力。
如果您要重写,您就会知道基类 Load 何时发生,因为您正在调用它。 这提供了更多的控制,但正如许多人认为的那样,这可能是一件坏事。
如果您使用事件,则无法保证调用顺序。 这迫使您编写 Load 事件,该事件应该不知道超类在 Load 阶段正在做什么。 我认为这将是首选方法,也许这就是 VS 自动生成代码采用这种方式的原因。
I think there is one potentially significant difference in the two methods.
What I am referring to is the ability to have control over the execution sequence.
If you are overriding, you know when the base classes Load will take place because you are calling it. This provides more control, but probably is a bad thing as many will argue.
If you use event, you have no guarantee in terms of order of call. This forces you to write Load event which should be agnostic about what super classes are doing during Load phase. I think this would be the preferred approach and maybe that is why VS auto-generated code is this way.
正如您在上面所看到的,如果选择是明智地做出的,那么它主要取决于个人选择。 我见过的最好的快速但可靠的概述位于 http://weblogs.asp.net/infinitiesloop/archive/2008/03/24/onload-vs-page-load-vs-load-event.aspx
As you can see above, it does mostly come down to personal choice IF that choice is made knowledgeably. The best quick but solid overview I've seen is at http://weblogs.asp.net/infinitiesloop/archive/2008/03/24/onload-vs-page-load-vs-load-event.aspx
这实际上只是一个选择的问题。 对我来说,对象将事件附加到自身似乎很奇怪,特别是当有一个可以重写的方法时。
我认为 ASP.NET 团队使用事件是因为这是 ASP 中 Global.asa 的模型,并且降低了不了解继承和重写虚拟方法的开发人员的门槛。
重写该方法确实需要更多有关页面生命周期的知识,但它没有任何“问题”。
It's really just a matter of choice. To me it seems weird for an object to attach an event to itself, especially when there is a method you can override.
I think the ASP.NET team used events because that was the model for Global.asa in ASP, and to lower the bar for developers who don't understand inheritance and overriding virtual methods.
Overriding the method does require more knowledge about the page lifecycle, but there is nothing "wrong" with it.
阅读标题为“ASP.NET Web 服务器控件事件模型”(页面链接)
有一些有用的语句,例如:
(AutoEventWireup 标志打开诸如 Page_Load 之类的方法)
Read the section called: "Binding Page Events" on the MSDN page titled: "ASP.NET Web Server Control Event Model" (link to the page)
There are some useful statements like these:
(AutoEventWireup flag turns on such methods like Page_Load)
即使您继承自
UserControl
,我认为如果不需要的话,您应该避免覆盖受保护的方法。Page_Load
可以让您更轻松地添加特定于UserControl
的代码。仅当您需要绝对控制何时触发
Load
事件时才重写OnLoad
(这应该很少见,IMO)。Even though you're inheriting from
UserControl
, I think you should stay away from overriding the protected methods if you don't have to. ThePage_Load
is there to make it easier for you to add the code that's specific to yourUserControl
.Only override
OnLoad
if you need absolute control over when(/if) theLoad
event is fired (which should be rare, IMO).