Last post Jul 02, 2011 02:50 AM by buaaytt
Jun 29, 2011 08:42 AM|buaaytt|LINK
For example, a page Index.cshtml has Layout defined at the top. In the layout page, there're 2 RenderPage calls to other 2 content pages, as well as the RenderBody call that's supposed to render the Index.cshtml itself. The layout page might also contain
call to RenderSection methods.
So, what's the exact execution order starting from the request to Index.cshtml is received by ASP.NET?
Jun 29, 2011 02:39 PM|Ved Prakash Nunia|LINK
General page life cycle
--- The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can
be sent in response without running the page.
-----In the start stage, page properties such as Request and
Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the
IsPostBack property. The page also sets the
----- 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.
---- During load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.
Post back event handling
---If the request is a postback, control event handlers are called. After that, the
Validate method of all validator controls is called, which sets the
IsValid property of individual validator controls and of the page.
---Before rendering, view state is saved for the page and all controls. During the rendering stage, the page calls the
Render method for each control, providing a text writer that writes its output to the
OutputStream object of the page's
---The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such
as Response and
Request are unloaded and cleanup is performed.
Jun 29, 2011 05:03 PM|buaaytt|LINK
Thanks Ved, but I don't think the page lifecycle of asp.net web form is the same as that in asp.net mvc. After all, there's no concepts like RenderBody and RenderSection in asp.net web form.
Basically the root cause of this thread is from some code I saw that involves the use of PageData. So I could set PageData value in content page, and then in Layout page I can check the value of PageData and display differently based on that value. This
has made me think about the execution order.
Here's an example:
In content page:
Layout = "...";
PageData["show"] = true;
In Layout page:
if (PageData["show"] == true)
This looks great but makes me confused. When exactly is Layout page being parsed? I guess it should be when the Layout property setter code is encountered. But if then asp.net shifts to the Layout page, the PageData hasn't yet been set, so it shouldn't display
the layout page's dynamic part. But the fact is it just works.
Anyone can shed some light on this?
Jun 29, 2011 06:21 PM|pranavkm|LINK
Jul 01, 2011 04:07 PM|buaaytt|LINK
Thanks, but I don't think this answers my question of how/when the PageData value set in content page gets passed to Layout page during execution.
Now I got some suspects. I guess once ASP.NET meets the Layout setter, it will check all the PageData defined in all content pages and the Layout page and merge them into a single page data dictionary. Only this can explain why in content pages I set PageData
after the Layout setter, but the Layout page can still recognize the PageData.
Jul 01, 2011 05:03 PM|pranavkm|LINK
As I mentioned earlier, the layout page is executed at the very end and a copy of the PageData is passed to it. Have a look at WebPageBase in a disassembler, the code looks something like this:-
var pageContent = page.Execute();
var layoutPage = GetLayoutPage(page.LayoutPage);
layoutPage.PageData = content.PageData.Clone();
layoutPage.BodyContent = pageContent;
Jul 02, 2011 02:50 AM|buaaytt|LINK
I can't find the code in the downloaded MVC3 rtm source code, so I reckon the code you pasted here is for previous version of MVC. But it enlightens me a lot. Thanks.