Page Events in Asp.Net
In ASP.Net, the client browser will make a request to get a web page. After displaying the page, user can do further navigation via the displayed webpage. In the server side, the server will raise multiple events during the life cycle of a web page. Some of the page events are below:
- Page Init: This stage Initializes the page with controls. If any theme needs to be applied, it will be applied here in this phase.
- Load: This stage loads the web page with all the controls. In case of post-back, web server will set control properites from the view state. We will learn more about view state in some other article.
- Validate: Uses Validator Control and runs validation.
- Event Handle: Runs the post-back events. Note, in web application the event happens on the clients browser and its event routines will run on the server at later stage. For Example, let us say a browsing user triggers the text_changed event on the browser end. The event handler routine for the text_changed will run on the server when the user submites text field containing Web-Form or if a post-back occurred. We will learn about post back later.
- Render: In this stage, rendering the page for the web -browser occurs. It is a dynamic html generation stage.
- Unload: This stage denotes the unloading of the page from the Server memory. After this stage the page becomes no more.
We can add an event handler on any of the above stage and wire-up our code to run. For example, one can add some code in the Load event handler to do additional initialization. Note, the Page Init runs before the Load and hance it is a perfect place to override the default initialization. In this tutorial, we will handle some of the page events and see in what order ASP.Net fires the page events.
1: Create Web Site
Here, we will create a website and delete most of the default.aspx content. Follow the marked steps below:
1) After creating the website called 03_PageEvents (Creating a website was already explained in the previous article. So here, we will keep it as a single step) double click the Default.aspx file to open it.
2) Remove the entry MasterPageFile. We will learn more about it in some other step-by-step tutorial.
3) In this sub-step, highlight the content and delete them as well. After modification, the page content will be reduced into a few lines.

2: Provide Page_Load Handler
The Asp.Net web server calls the Page_Load handler function to perform control initialization with properties. In this step, we will add empty skeleton for the Page_Load handler function.
1) In the Solution explorer, expand the arrow button. It reveals the code-behind file with .cs extension.
2) Double click to open the Default.aspx.cs file
3) In the code editor window, add the code for Page_Load
. Asp.Net web Server will call whatever we write inside this handler when page load happens.
3: Add Page_Load Handler Code
1) The
System.Diagnostics namespace will help us to write diagnostics code. Here, we included the namespace to write some trace messages to the output window.
2) The
WriteLine method will write the text string to the console window. Here, we just stated that Page Load is called by the server code. This trace message will be useful when we are launching the web page on the server. When we see the message in the output window, we can conclude that Page Load happend on the server.

4: Add Other Page Handler Function
In the below code snippet, we added handler code for other Page Life-Cycle events. A quick introduction about all these events are in the first section of this tutorial. In all these handlers, we have debug print messages, and these messages will help us to experiment in which order the web server is firing the page life cycle events.

5: Run the Website
1) From the toolbar click on the debug icon button. This will launch the web site’s web page default.aspx. Since, we removed all the content, it will not see anything in the browser.
2) To see in which order the Webserver fires the Page events, bring-in the Output Window from the view menu.
3) The output window shows in what order the Page events are fired. The handler function tied to these events printed the message as & when the events are fired.

Code Reference
1) Default.aspx
1 2 |
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
2) Default.aspx.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; //Sample 01: Namespace Inclusion using System.Diagnostics; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //Sample 02: Page Load Event Debug.WriteLine("Page Load Handler Invoked"); } //Sample 03: Other Page Event protected void Page_UnLoad(object sender, EventArgs e) { Debug.WriteLine("Page UnLoad Handler Invoked"); } protected void Page_Init(object sender, EventArgs e) { Debug.WriteLine("Page Init Handler Invoked"); } protected void Page_PreRender(object sender, EventArgs e) { Debug.WriteLine("Page PreRender Handler Invoked"); } } |
Categories: ASP 4.0
Tags: Page_Init, Page_Load, Page_PreRender, Page_Unload, System.Diagnostics, WriteLine