Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of different order of execution: Init() and Load()

Tags:

asp.net

events

During a recent interview the following question was asked.

• 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

The Init Event will fire (Inner Most to Outer Most)

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

and Load Event will fire

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

Why does ASP.NET framework support different execution order in Load() and Init().This was the question asked in interview.I have no idea about what the interviewer expecting from me.

I request your help please.

like image 860
ASP.netBeginner Avatar asked Dec 22 '22 19:12

ASP.netBeginner


2 Answers

I suggest reading about the ASP.NET page life cycle.

The two have different purposes, hence different execution order.

Initialization :

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.

Load:

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

Additionally, you need to understand the relationship between master pages and content pages (master pages are in fact included in the content pages, not the other way around) and the complete life cycle of both.


So, during init, the user controls need to be initialized first, so they are available to their container, then the master page so it's contents are available to the content page and then the page itself, completing the control hierarchy initialization.

During load, the opposite happens, as now all postback data has been set and all the controls are ready and can fire their different events. The top container, the content page loads first (as it can change the master page and user controls), then the master page and in the end the leaf controls.

like image 154
Oded Avatar answered Dec 28 '22 05:12

Oded


The reason is for control management. Sometimes you need to create controls dynamically and in order for them to work correctly, you need to re-create them on the init and not the onload(). If you don't re-create the controls onInit, you're dynamic controls will not working properly.

like image 22
user1981391 Avatar answered Dec 28 '22 07:12

user1981391