Sunday, 26 April 2009

Magic of Page Load and Page Init execution.

Problem:

Not completely understood the Page Life Cycle in ASP.NET.

This is very helpful in big projects, that we need to initialize some object in Master page, page load and try to access the same object in aspx page load, it will give error [Because aspx page load will execure first.]. In this type of problems you faced, please write the same code in init of the master and aspx pages.
Scenario I,
If we have a page called "Default" and a Master page called "DefaultMaster".
Both contains Page_Load events.
When a page request comes to Default page, which Page Load event will call first?
Answer: default.aspx page load.
Scenario II,
The same pages as above with Page_Init event is also present.
Now which Page_Init will call first by .net runtime?
Answer: Master page init event, but not default.aspx page init event.
This is the good point to know that,
All page Init events will process from master page to aspx pages and all page Load events will process from aspx pages to master pages.

Page Init EventPage Load event
1. Execution sequence starts from Init event of Main master page--> Sub Master Page --> ……. -->Requested ASPX page init event.1. Execution sequence starts from Load event of Requested ASPX page --> Sub Master Page --> ……. --> Main master page load event.


Whole Page Life Cycle which contains both events
Master page Init --> Sub Master Page Init --> …. --> ASPX Init --> ASPX Load--> …. --> Sub Master Page Load --> Master page Load.

No comments:

Post a Comment