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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With