As we all know that while serving an asp.net request goes through many stages before it gets served. One of the important stages is, going through the page life cycle events. As a page can contain many other controls like User Control, Custom control etc and it can be a combination of master and content pages as well. As each control has its own life cycle so if they are inter weaved then it becomes very important to understand that how each event fits in complete lifecycle . Also this is one of the most asked question in asp.net interviews so I thought of explaining it.
Technically, Master page is nothing but a normal user control so you can treat it as user control used on a page and we will consider it same in our post. So we can say that there are two type of controls – User Control and Custom Control in a page. Content page is also nothing but a normal aspx page. Now to understand the order of events where all of these controls are available, lets first understand of the life cycle of each components individually.
We can see that Pre_init is available in only in Page life cycle but not in the other controls and rest are same. Let’s see some example with various combination of these controls.
So in our next example, I am using a page which contains a master page and a user control (user control 1) on content page. I have implemented all the events that we discussed in the life cycle of the controls (Master page, content page, user control) and in each event I am writing in a log file when event is fired, to depict the proper flow of the page. So let’s see the flow of the page when it runs
Here, we see that it starts from Page_PreInit which is page life cycle’s first event then we see the next three events numbered 2,3,4 (first red encircled) then here first user control’s init gets called then comes master page and last one is Page_Init. After that we see the Load events of the components. For Load, the flow is different and starts from the page, master page and at last user control. Similar is also followed for PreRender and SaveViewState events. But again if we see the last three Unload events then we find it again fired in reversed order (same as Init events). So the key observations here that Init and Unload events get fired in reverse order and rest in normal as they put in the page. Other key item is that master page events gets fired just after ( or before for Init and Unload events) after the page events.
Now lets take another scenario. Here I am adding two more components – one more user control (user control -2 ) and a custom control. The custom control is placed in user control 2 and it is placed in user control 1 itself. Now I am going to run the application and show you the order of events
so here again if we closely see then we find that Init and Unload events are fired in reverse (inner most to outer most) order and rest are called from (outer to inner). For other events, master is always second and others outer to inner. If there are two controls are at same level ( like there are two controls on a page) then which appear first, gets called first.
So we now understand the ordering logic of life cycle events but now the question arises why the order of the Init and Unload are called in opposite order? Let’s try to understand
Why Init and Unload called in reverse order?
In simple words, we access the controls of the page many times in our page like in second example, I can find the instance of custom control in Page_Load event or in any other event of the page ( here we know the custom control is part of the user control 2 which itself is in user control 1) so if we try to find the custom control in Page_Init (and if it is fired first) the we wont be able to find because it is not initialized which will be an issue because we wont be able to make any change while page initialization. Similarly, Unload events start from the innermost control and it is also desirable because we might want to unload or want to dispose some items which are part of user control so we can do the same from Page_Unload and say if Page itself first gets unload then it may happen that other control might not be disposed properly.
We also know that controls are property of Page class so once a request enters to page life cycle then page structure gets created (In Pre_Init) and some basic structural initialization occurs for the page then actual initialization starts which starts from the inner most control. After initialization only the controls can be used properly. Similarly in case unload first the property of the page i e control gets unloaded then Page itself.
I hope you know the ordering of the events and the logic behind it. Thanks for reading this post and please share your feedback or if any specific items you wan to see.