i was trying to find the sequence of event between Master page and aspx page.
I came to know that unload event of master page calls before then unload event of ASPX page.
I already read this in many websites but i am looking for reason behind it.
If someone has an idea about this then kindly help me.
One good reason I found is this::
Master pages behave like child controls on a page.
What this means is that the way any Control events are raised by the Asp.Net page life cycle, same way it will raise events for Master pages.
Consider for example, the Init event. MSDN says that the Init event of individual controls occurs before the Init event of the page. And thus, the Init event of Master page occurs before the Init event of page.
One more example, consider the Load event.MSDN says that the Load event of individual controls occurs after the Load event of the page. Therefore the Load event of Master page occurs after the load event of content page.
This is indeed confirmed by MSDN::
Master pages behave like child controls on a page: the master page Init event
occurs before the page Init and Load events, and the master page Load event
occurs after the page Init and Load events
So, now you can say that the way Unload event is raised for controls, it will be same for master page.
The Unload event is raised for each control first and then for the page. So this is the reason why Unload event of master page is called before Content page.
Related
My understanding of the page life cycle is as follows:
Page Page_PreInit
MasterPage Page_Init
Page Page_Init
Page Page_InitComplete
Page Page_PreLoad
Page Page_Load
MasterPage Page_Load
Page_LoadComplete
Page_PreRender
MasterPage Page_PreRender
Page Page_PreRenderComplete
Page Page_SaveStateComplete
MasterPage Page_Unload
Page Page_Unload
Given the above, where does the MasterPage's Load event fire? Google isn't helping as quite a lot of people say Load when talking about the OnLoad event.
Master Page load event is between Content page Load event and Master page controls Load event.
Check the MSDN for details
Although both Init and Load recursively occur on each control, they
happen in reverse order. The Init event (and also the Unload event)
for each child control occur before the corresponding event is raised
for its container (bottom-up). However the Load event for a container
occurs before the Load events for its child controls (top-down).
Master pages behave like child controls on a page: the master page
Init event occurs before the page Init and Load events, and the master
page Load event occurs after the page Init and Load events.
MSDN
The following is the sequence in which events occur when a master page is merged with a content page:
Content page PreInit event.
Master page controls Init event.
Content controls Init event.
Master page Init event.
Content page Init event.
Content page Load event.
Master page Load event.
Master page controls Load event.
Content page controls Load event.
Content page PreRender event.
Master page PreRender event.
Master page controls PreRender event.
Content page controls PreRender event.
Master page controls Unload event.
Content page controls Unload event.
Master page Unload event.
Content page Unload event.
Source: https://msdn.microsoft.com/en-us/library/dct97kc3%28v=vs.140%29.aspx?f=255&MSPPError=-2147217396
Can someone explain to me the sheer purpose of a page load? My code runs just fine without it right now in my aspx.cs (codebehind) file. I am doing very basic stuff here, so im guessing it has a lot of importance somewhere so i am just wondering what that would be. thanks for any help!
You should check about the Page Life Cycle.
The load is an event in this Cycle.
About the method, Page_load() is the method on the server side application, for an .aspx file. All code inside of this method is executed once at the beginning of the page.
Also, in the load, if the current request is a postback, control properties are loaded with information recovered from view state and control state. (Different from initialize, when you set the default values)
So, in the Load Event, the Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.
Some links for you:
ASP .NET Page Life Cycle
Page_Load and Postback
and there are a few more in Google
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.
I have a Master page and a webpage that uses the Master page.
In both I have a Page_Load event handler.
In which order are the Page_Load handlers called? Content first then Master or Master first then content?
The content page load event will fire before the master page load event.
See here for full order (MSDN: Events in ASP.NET Master and Content Pages)
Copying incase link goes dead:
The following is the sequence in which events occur when a master page is merged with a content page:
Content page PreInit event.
Master page controls Init event.
Content controls Init event.
Master page Init event.
Content page Init event.
Content page Load event.
Master page Load event.
Master page controls Load event.
Content page controls Load event.
Content page PreRender event.
Master page PreRender event.
Master page controls PreRender event.
Content page controls PreRender event.
Master page controls Unload event.
Content page controls Unload event.
Master page Unload event.
Content page Unload event.
As far as MasterPage is indeed a user control all rules applied to it as for user controls:
Master pages run Page_Load() from the deepest level of nesting outwards. So your nested webpage will first run the page_load event.
When loading a page for the first time (!IsPostback), I am creating a button in code and adding it to my page, then adding an event handler to the click event.
However, when clicking the button, after the page reloads, my event handler does not fire.
Can anyone explain why?
#Brad: Your answer isn't complete; he's most likely doing it too late in the page lifecycle, during the Page_Load event.
Okay, here's what you're missing.
ASP.NET is stateless. That means, after your page is rendered and sent to the browser, the page object and everything on it is destroyed. There is no link that remains on the server between that page and what is on the user's browser.
When the user clicks a button, that event is sent back to the server, along with other information, like the hidden viewstate field.
On the server side, ASP.NET determines what page handles the request, and rebuilds the page from scratch. New instances of server controls are created and linked together according to the .aspx page. Once it is reassembled, the postback data is evaluated. The viewstate is used to populate controls, and events are fired.
This all happens in a specific order, called the Page Lifecycle. In order to do more complex things in ASP.NET, such as creating dynamic controls and adding them to the web page at runtime, you MUST understand the page lifecycle.
With your issue, you must create that button every single time that page loads. In addition, you must create that button BEFORE events are fired on the page. Control events fire between Page_Load and Page_LoadComplete.
You want your controls loaded before ViewState information is parsed and added to controls, and before control events fire, so you need to handle the PreInit event and add your button at that point. Again, you must do this EVERY TIME the page is loaded.
One last note; page event handling is a bit odd in ASP.NET because the events are autowired up. Note the Load event handler is called Page_Load...
You need to add the button always not just for non-postbacks.
If you are not reattaching the event handler on every postback, then the event will not exist for the button. You need top make sure the event handler is attached every time the page is refreshed. So, here is the order of events for your page:
Page is created with button and event handler is attached
Button is clicked, causing a postback
On postback, the page_load event skips the attaching of the event handler becaue of your !IsPostback statement
At this point, there is no event handler for the button, so clicking it will not fire your event
That is because the event binding that happens needs to be translated in to HTML. This postback that happens if bound to the page between OnInit and OnLoad. So if you want the button to bind events correclty make sure you do the work in OnInit.
See the Page Life Cycle explaination.
http://msdn.microsoft.com/en-us/library/ms178472.aspx