c# - page_load 和 onLoad 的区别

标签 c# asp.net .net code-behind

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

最佳答案

您可能应该阅读 Page Lifecycle Overview了解更多信息。

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

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.

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.

OnLoad 是页面的一部分并且总是被调用。您不需要 Page_Load 方法,它只是事件的可选扩展。

关于c# - page_load 和 onLoad 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3464898/

相关文章:

c# - 将 key 转换为 C# 中的 key

asp.net - 使用 ASP.NET Core 2.0 进行用户管理

.net - Google.Apis.Requests.RequestError 需要登录 [401] 消息[需要登录] 位置

c# - 通用 WinRT 应用程序中的 AppBarButton 异常未触发未处理的异常事件

c# - 如何在 MVC 中使用 Dapper 插入数据并返回插入的标识?

c# - 尝试更改 DataGrid 中的单元格颜色时出现 WPF 表单中的 InvalidOperationException

c# - 将 JSON 数据对象存储到 ASP.NET MVC( Entity Framework )中的数据库

asp.net - 通过Ajax向 Controller 发送数据

asp.net - 为什么我无法验证其中包含 $ 符号的值?

c# - 在Hashtable中打印的顺序?