Suggested videos before continuing with this session
Part 3 - Understanding ViewState
Part 4 - Events in the life cycle of a web application
Part 5 - Difference between ViewState, SessionState and ApplicationState
In Part 4, of this video series, we have discussed that, events can occur at 3 levels in an asp.net web application.
1. At the application level. (Example - Session_Start event in global.asax)
2. At the Page or web form level (Example - Page_Load)
3. At the control level(Example - Selected Index changed event of a dropdownlist)
In this video, we will discuss about events at the page level. From the previous,parts of this video series, we have learnt that, web applications work on a stateless protocol. Every time a request is made for a webform, the following sequence of events occur.
1. Web Application creates an instance of the requested webform.
2. Processes the events of the webform.
3. Generates the HTML, and sends the HTML back to the requested client.
4. The webform gets destroyed and removed from the memory.
The following are some of the commonly used events in the life cycle of an asp.net webform. These events are shown in order of occurrence, except for, Error event, which occurs only if there is an unhandled exception.
PreInit - As the name suggests, this event happens just before page initialization event starts. IsPostBack, IsCallback and IsCrossPagePostBack properties are set at this stage. This event allows us to set the master page and theme of a web application dynamically. PreInit is extensively used when working with dynamic controls.
Init - Page Init, event occurs after the Init event, of all the individual controls on the webform. Use this event to read or initialize control properties. The server controls are loaded and initialized from the Web form’s view state.
InitComplete - As the name says, this event gets raised immediately after page initialization.
PreLoad - Happens just before the Page Load event.
Load - Page Load event, occurs before the load event of all the individual controls on that webform.
Control Events - After the Page load event, the control events like button's click, dropdownlist's selected index changed events are raised.
Load Complete - This event is raised after the control events are handled.
PreRender - This event is raised just before the rendering stage of the page.
PreRenderComplete - Raised immediately after the PreRender event.
Unload - Raised for each control and then for the page. At this stage the page is, unloaded from memory.
Error - This event occurs only if there is an unhandled exception.
To see the events execution order, create a new asp.net web project, and copy the following code.
protected void Page_PreInit(object sender, EventArgs e)
{ Response.Write("Page_PreInit" + "<br/>"); }
protected void Page_Init(object sender, EventArgs e)
{ Response.Write("Page_Init" + "<br/>"); }
protected void Page_InitComplete(object sender, EventArgs e)
{ Response.Write("Page_InitComplete" + "<br/>"); }
protected void Page_PreLoad(object sender, EventArgs e)
{ Response.Write("Page_PreLoad" + "<br/>"); }
protected void Page_LoadComplete(object sender, EventArgs e)
{ Response.Write("Page_LoadComplete" + "<br/>"); }
protected void Page_PreRender(object sender, EventArgs e)
{ Response.Write("Page_PreRender" + "<br/>"); }
protected void Page_PreRenderComplete(object sender, EventArgs e)
{ Response.Write("Page_PreRenderComplete" + "<br/>"); }
protected void Page_Unload(object sender, EventArgs e)
{
//Response.Write("Page_Unload" + "<br/>");
}
Run the project and you should see the following output.
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
Note: If you uncomment, Response.Write() method call in Page_Unload() event, you get System.Web.HttpException stating - Response is not available in this context. This makes sense because, the Unload event is raised after the page has been fully rendered, and the HTML is already sent to the client. At this stage, the webform instance is ready to be discarded. So, at this point, page properties such as Response and Request are unloaded and clean up is performed.
Part 3 - Understanding ViewState
Part 4 - Events in the life cycle of a web application
Part 5 - Difference between ViewState, SessionState and ApplicationState
In Part 4, of this video series, we have discussed that, events can occur at 3 levels in an asp.net web application.
1. At the application level. (Example - Session_Start event in global.asax)
2. At the Page or web form level (Example - Page_Load)
3. At the control level(Example - Selected Index changed event of a dropdownlist)
In this video, we will discuss about events at the page level. From the previous,parts of this video series, we have learnt that, web applications work on a stateless protocol. Every time a request is made for a webform, the following sequence of events occur.
1. Web Application creates an instance of the requested webform.
2. Processes the events of the webform.
3. Generates the HTML, and sends the HTML back to the requested client.
4. The webform gets destroyed and removed from the memory.
The following are some of the commonly used events in the life cycle of an asp.net webform. These events are shown in order of occurrence, except for, Error event, which occurs only if there is an unhandled exception.
PreInit - As the name suggests, this event happens just before page initialization event starts. IsPostBack, IsCallback and IsCrossPagePostBack properties are set at this stage. This event allows us to set the master page and theme of a web application dynamically. PreInit is extensively used when working with dynamic controls.
Init - Page Init, event occurs after the Init event, of all the individual controls on the webform. Use this event to read or initialize control properties. The server controls are loaded and initialized from the Web form’s view state.
InitComplete - As the name says, this event gets raised immediately after page initialization.
PreLoad - Happens just before the Page Load event.
Load - Page Load event, occurs before the load event of all the individual controls on that webform.
Control Events - After the Page load event, the control events like button's click, dropdownlist's selected index changed events are raised.
Load Complete - This event is raised after the control events are handled.
PreRender - This event is raised just before the rendering stage of the page.
PreRenderComplete - Raised immediately after the PreRender event.
Unload - Raised for each control and then for the page. At this stage the page is, unloaded from memory.
Error - This event occurs only if there is an unhandled exception.
To see the events execution order, create a new asp.net web project, and copy the following code.
protected void Page_PreInit(object sender, EventArgs e)
{ Response.Write("Page_PreInit" + "<br/>"); }
protected void Page_Init(object sender, EventArgs e)
{ Response.Write("Page_Init" + "<br/>"); }
protected void Page_InitComplete(object sender, EventArgs e)
{ Response.Write("Page_InitComplete" + "<br/>"); }
protected void Page_PreLoad(object sender, EventArgs e)
{ Response.Write("Page_PreLoad" + "<br/>"); }
protected void Page_LoadComplete(object sender, EventArgs e)
{ Response.Write("Page_LoadComplete" + "<br/>"); }
protected void Page_PreRender(object sender, EventArgs e)
{ Response.Write("Page_PreRender" + "<br/>"); }
protected void Page_PreRenderComplete(object sender, EventArgs e)
{ Response.Write("Page_PreRenderComplete" + "<br/>"); }
protected void Page_Unload(object sender, EventArgs e)
{
//Response.Write("Page_Unload" + "<br/>");
}
Run the project and you should see the following output.
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete
Note: If you uncomment, Response.Write() method call in Page_Unload() event, you get System.Web.HttpException stating - Response is not available in this context. This makes sense because, the Unload event is raised after the page has been fully rendered, and the HTML is already sent to the client. At this stage, the webform instance is ready to be discarded. So, at this point, page properties such as Response and Request are unloaded and clean up is performed.
Great Information and I specially like your discuss about events at the page level.
ReplyDeleteyou are like god to me...you done my day..thank you sir. god bless you.
ReplyDeleteclick on the image which says, click here for ASP.NET Videos. This will take you to the youtbe playlist, where you can watch.
DeleteHello Venkat Sir, You are Truly a Great Tutor, I must say you are more than GOD o me.
ReplyDeleteThanks,God Bless You Sir.
Hi Venkat
ReplyDeleteIn MSDN, there is SaveStateComplete and Render events. Please explain these also. Besides this, please explain that what kind of things can be done in PreLoad, Loadcomplete, Prerender etc. Thanks.
just comment the Line inside Page_Unload event ,because Webform is get destroyed after rendering so Response and request properties are not available .
Deletewhy i am facing this problem "Response is not available in this context." when i copy given code and execute,
ReplyDeletegod bless you venkat............
ReplyDeleteI am very much confused, how can we declare all page life cycle events in our page, since it is maintained by system to handle the web pages.
ReplyDeletePage_PreInit event occurs before page initialization, how can we declare Page_PreInit event in page level
Thank you so much sir you are definitely The best Instructor on the web
ReplyDeleteHello Venkate,
ReplyDeleteI'm big fan of you.
Can you please make video for SOLID Principles with an examples
Thanks in advance