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

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 

An Init event will be generated (most commonly used inner majority)

 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 the ASP.NET framework support a different execution order in Load() and Init() . That was the question asked in the interview. I have no idea what the interviewer expects from me.

I apologize.

+4
source share
2 answers

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

Both have different goals, therefore, a different order of execution.

Initialization:

During page initialization, the controls on the page are available and each UniqueID control is installed. In addition, the master page and themes are also applicable on the page. If the current request is postback, postback data has not yet been loaded, and control property values ​​have not been restored to values ​​from the view state.

Load:

At boot time, if the current request is reverse processing, control properties are loaded with information recovered from the view state and control state.

In addition, you need to understand the relationship between the main pages and the content pages (the main pages are actually included in the content pages, and not vice versa) and the full life cycle of both .


So, during initialization, you must first initialize the user controls, so they are available for their container, and then the main page so that the content is available on the content page, and then the page itself, completing the initialization of the control hierarchy.

At boot time, the opposite happens, since now all the data of the reverse gear is installed and all the controls are ready and can trigger different events. The top container, the content page is loaded first (since it can change the main page and user controls), then the main page and at the end of the sheet control.

+10
source

The reason is management management. Sometimes you need to dynamically create controls, and for them to work correctly, you need to recreate them in init, not onload (). If you do not re-create the onInit controls, you will not work with dynamic controls.

+1
source

Source: https://habr.com/ru/post/1335867/


All Articles