Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

ASP 4.0 – Understanding Page Events | Lab #003

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:

  1. Page Init: This stage Initializes the page with controls. If any theme needs to be applied, it will be applied here in this phase.
  2. 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.
  3. Validate: Uses Validator Control and runs validation.
  4. 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.
  5. Render: In this stage, rendering the page for the web -browser occurs. It is a dynamic html generation stage.
  6. 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.

Step 1 - Create Project and Empty Default
Step 1 – Create Project and Empty Default

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.

Step 2 - Open Code Behind for Default.aspx Page
Step 2 – Open Code Behind for Default.aspx Page

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.

Step 3 - Write Page Load Handler Function
Step 3 – Write Page Load Handler Function

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.

Step 4 - Write Other Page Handler Functions
Step 4 – Write Other Page Handler Functions

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.

Step 5 - Start Debug and Observe the Output
Step 5 – Start Debug and Observe the Output

Code Reference

1) Default.aspx

2) Default.aspx.cs

Categories: ASP 4.0

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.