Commonly used Page Events in ASP.NET

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.

  1. protected void Page_PreInit(object sender, EventArgs e)   
  2. {  
  3.     Response.Write("Page_PreInit" + "<br/>");  
  4. }  
  5.   
  6. protected void Page_Init(object sender, EventArgs e)   
  7. {  
  8.     Response.Write("Page_Init" + "<br/>");  
  9. }  
  10.   
  11. protected void Page_InitComplete(object sender, EventArgs e)   
  12. {  
  13.     Response.Write("Page_InitComplete" + "<br/>");  
  14. }  
  15.   
  16. protected void Page_PreLoad(object sender, EventArgs e)  
  17. {  
  18.     Response.Write("Page_PreLoad" + "<br/>");  
  19. }  
  20.   
  21. protected void Page_LoadComplete(object sender, EventArgs e)   
  22. {  
  23.     Response.Write("Page_LoadComplete" + "<br/>");  
  24. }  
  25.   
  26. protected void Page_PreRender(object sender, EventArgs e)  
  27. {  
  28.     Response.Write("Page_PreRender" + "<br/>");  
  29. }  
  30.   
  31. protected void Page_PreRenderComplete(object sender, EventArgs e)   
  32. {  
  33.     Response.Write("Page_PreRenderComplete" + "<br/>");  
  34. }  
  35.   
  36. protected void Page_Unload(object sender, EventArgs e)   
  37. {  
  38.   
  39.     //Response.Write("Page_Unload" + "<br/>");  
  40. }  
Run the project and you should see the following output.

Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete

Ebook Download
View all
Learn
View all