加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 不同执行顺序的目的:Init()和Load()

发布时间:2020-12-16 03:57:33 所属栏目:asp.Net 来源:网络整理
导读:在最近的一次采访中,提出了以下问题. ? A Master page which contains ? An ASPX web form page which contains ? A Web User Control inside the page which contains ? A button to fire some code in a button_click event 初始事件将触发(内部最多到外部
在最近的一次采访中,提出了以下问题.

? A Master page which contains 
   ? An ASPX web form page which contains 
      ? A Web User Control inside the page which contains 
         ? A button to fire some code in a button_click event

初始事件将触发(内部最多到外部最多)

aspx.page Begin Init
   –> Inside user control Page_Init 
   –> Inside master page Page_Init
   –> Inside lifecycle page Page_Init 
aspx.page End Init

和Load事件将触发

aspx.page Begin Load
  –> Inside lifecycle page Page_Load
  –> Inside master page Page_Load
  –> Inside user control Page_Load 
aspx.page End Load

为什么ASP.NET框架在Load()和Init()中支持不同的执行顺序.这是面试中提出的问题.我不知道面试官对我的期望.

我请求你的帮助.

解决方法

我建议阅读有关 ASP.NET page life cycle的内容.

两者有不同的目的,因此执行顺序不同.

初始化:

During page initialization,controls on the page are available and each control’s UniqueID property is set. A master page and themes are also applied to the page if applicable. If the current request is a postback,the postback data has not yet been loaded and control property values have not been restored to the values from view state.

加载:

During load,if the current request is a postback,control properties are loaded with information recovered from view state and control state.

此外,您需要了解母版页和内容页之间的relationship(母版页实际上包含在内容页中,而不是相反)和complete life cycle of both.

因此,在init期间,需要首先初始化用户控件,因此它们可用于其容器,然后是主页,因此它的内容可用于内容页面,然后是页面本身,从而完成控件层次结构初始化.

在加载过程中,相反的情况发生了,因为现在所有的回发数据都已设置好并且所有控件都准备就绪并且可以触发它们的不同事件.顶部容器,内容页面首先加载(因为它可以更改母版页和用户控件),然后是主页面,最后是叶子控件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读