创建 Web 控件时应该重写 OnLoad 还是实现 Page_Load

发布于 2024-07-05 14:15:47 字数 166 浏览 11 评论 0原文

当您在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

拒绝两难 2024-07-12 14:15:47

OnLoad 方法应该是引发 Load 事件的地方。 我个人总是尝试处理该事件,除非我需要围绕引发事件进行额外的处理。

我建议在正常情况下自行处理事件。

The OnLoad method should be the place where the Load 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.

此岸叶落 2024-07-12 14:15:47

我认为是一样的。
恕我直言,通过事件,您可以拥有更多的灵活性,因为您的事件可以有多个侦听器!

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!

一梦浮鱼 2024-07-12 14:15:47

我认为这两种方法存在一个潜在的显着差异。

我指的是控制执行顺序的能力。

如果您要重写,您就会知道基类 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.

眉黛浅 2024-07-12 14:15:47

正如您在上面所看到的,如果选择是明智地做出的,那么它主要取决于个人选择。 我见过的最好的快速但可靠的概述位于 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

红尘作伴 2024-07-12 14:15:47

这实际上只是一个选择的问题。 对我来说,对象将事件附加到自身似乎很奇怪,特别是当有一个可以重写的方法时。

我认为 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.

[旋木] 2024-07-12 14:15:47

阅读标题为“ASP.NET Web 服务器控件事件模型”(页面链接
有一些有用的语句,例如:

AutoEventWireup 属性的一个缺点是它要求页面事件处理程序具有特定的、可预测的名称。 这限制了您命名事件处理程序的灵活性。 另一个缺点是性能受到不利影响,因为 ASP.NET 在运行时搜索方法。 对于流量较高的网站,对性能的影响可能会很大。

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:

One disadvantage of the AutoEventWireup attribute is that it requires that the page event handlers have specific, predictable names. This limits your flexibility in how you name event handlers. Another disadvantage is that performance is adversely affected, because ASP.NET searches for methods at run-time. For a Web site with high traffic volumes, the impact on performance could be significant.

(AutoEventWireup flag turns on such methods like Page_Load)

韵柒 2024-07-12 14:15:47

即使您继承自 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. The Page_Load is there to make it easier for you to add the code that's specific to your UserControl.

Only override OnLoad if you need absolute control over when(/if) the Load event is fired (which should be rare, IMO).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文