page_load和onLoad之间的区别

时间:2021-01-20 20:11:30

What is difference between page_load and onLoad functions in ASP.NET codebehind?

ASP.NET代码隐藏中的page_load和onLoad函数有什么区别?

4 个解决方案

#1


5  

Load is the event and OnLoad is a method that raises that event when called it's just base class implementation that does it of course, and therefore needs to be called from deriving classes so that events work)

Load是事件,OnLoad是一个在调用时引发该事件的方法,它当然是基类实现,因此需要从派生类调用以使事件起作用)

#2


21  

You should probably read the Page Lifecycle Overview for more info.

您应该阅读页面生命周期概述以获取更多信息。

This little bit should help clear up the difference:

这一点应该有助于消除差异:

Note that when an event handler is created using the Page_event syntax, the base implementation is implicitly called and therefore you do not need to call it in your method. For example, the base page class's OnLoad method is always called, whether you create a Page_Load method or not. However, if you override the page OnLoad method with the override keyword (Overrides in Visual Basic), you must explicitly call the base method. For example, if you override the OnLoad method on the page, you must call base.Load (MyBase.Load in Visual Basic) in order for the base implementation to be run.

请注意,使用Page_event语法创建事件处理程序时,将隐式调用基本实现,因此您无需在方法中调用它。例如,无论您是否创建Page_Load方法,始终都会调用基页类的OnLoad方法。但是,如果使用override关键字(在Visual Basic中覆盖)覆盖页面OnLoad方法,则必须显式调用基本方法。例如,如果在页面上覆盖OnLoad方法,则必须调用base.Load(Visual Basic中的MyBase.Load)才能运行基本实现。

and

Pages also support automatic event wire-up, meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised. If the AutoEventWireup attribute of the @ Page directive is set to true, page events are automatically bound to methods that use the naming convention of Page_event, such as Page_Load and Page_Init.

Pages还支持自动事件连接,这意味着ASP.NET会查找具有特定名称的方法,并在引发某些事件时自动运行这些方法。如果@Page指令的AutoEventWireup属性设置为true,则页面事件将自动绑定到使用Page_event命名约定的方法,例如Page_Load和Page_Init。

The OnLoad is part of the page and is always called. You don't need to have a Page_Load method which is just optional extension of the event.

OnLoad是页面的一部分,始终被调用。您不需要具有Page_Load方法,该方法只是事件的可选扩展。

#3


2  

They handle the same event but Page_Load() works only when AutoEventWireup="true".

它们处理相同的事件,但Page_Load()仅在AutoEventWireup =“true”时有效。

#4


2  

OnLoad fires the Load event, which Page_Load is a default event handler.

OnLoad触发Load事件,其中Page_Load是默认的事件处理程序。

#1


5  

Load is the event and OnLoad is a method that raises that event when called it's just base class implementation that does it of course, and therefore needs to be called from deriving classes so that events work)

Load是事件,OnLoad是一个在调用时引发该事件的方法,它当然是基类实现,因此需要从派生类调用以使事件起作用)

#2


21  

You should probably read the Page Lifecycle Overview for more info.

您应该阅读页面生命周期概述以获取更多信息。

This little bit should help clear up the difference:

这一点应该有助于消除差异:

Note that when an event handler is created using the Page_event syntax, the base implementation is implicitly called and therefore you do not need to call it in your method. For example, the base page class's OnLoad method is always called, whether you create a Page_Load method or not. However, if you override the page OnLoad method with the override keyword (Overrides in Visual Basic), you must explicitly call the base method. For example, if you override the OnLoad method on the page, you must call base.Load (MyBase.Load in Visual Basic) in order for the base implementation to be run.

请注意,使用Page_event语法创建事件处理程序时,将隐式调用基本实现,因此您无需在方法中调用它。例如,无论您是否创建Page_Load方法,始终都会调用基页类的OnLoad方法。但是,如果使用override关键字(在Visual Basic中覆盖)覆盖页面OnLoad方法,则必须显式调用基本方法。例如,如果在页面上覆盖OnLoad方法,则必须调用base.Load(Visual Basic中的MyBase.Load)才能运行基本实现。

and

Pages also support automatic event wire-up, meaning that ASP.NET looks for methods with particular names and automatically runs those methods when certain events are raised. If the AutoEventWireup attribute of the @ Page directive is set to true, page events are automatically bound to methods that use the naming convention of Page_event, such as Page_Load and Page_Init.

Pages还支持自动事件连接,这意味着ASP.NET会查找具有特定名称的方法,并在引发某些事件时自动运行这些方法。如果@Page指令的AutoEventWireup属性设置为true,则页面事件将自动绑定到使用Page_event命名约定的方法,例如Page_Load和Page_Init。

The OnLoad is part of the page and is always called. You don't need to have a Page_Load method which is just optional extension of the event.

OnLoad是页面的一部分,始终被调用。您不需要具有Page_Load方法,该方法只是事件的可选扩展。

#3


2  

They handle the same event but Page_Load() works only when AutoEventWireup="true".

它们处理相同的事件,但Page_Load()仅在AutoEventWireup =“true”时有效。

#4


2  

OnLoad fires the Load event, which Page_Load is a default event handler.

OnLoad触发Load事件,其中Page_Load是默认的事件处理程序。