Click here to Skip to main content
Click here to Skip to main content

Events on Web Page

, 24 Jan 2005
Rate this:
Please Sign up or sign in to vote.
Detecting web page events' sequence.

Introduction

By default, the web page comes with four basic events. They are init, load, prerender and the unload events. Only the first three events can be captured by code. We can not capture unload event because it happens when the content has been rendered on the client browser. That explains why there is no value kept at the second column on the last row of the table above.

Background

For someone who has just started learning ASP.NET, it is important to know the order in which these events page are triggered, before they step forward to learn further.

Using the code

I created the following code to show how to trap each event by code and the order in which they are called:

<%@ Page Language="C#"%>

<script Runat="server">

//declare one integer variable and set the starting 
value to 1
public int sequence = 1;

//It is trapping init event page
void Page_Init(Object o,EventArgs e)
{
    lblPageInit.Text = sequence.ToString();
    sequence++; //increase the sequence 
value by one every time this event is called
}

//It is trapping load event page
void Page_Load(Object o,EventArgs e)
{
    lblPageLoad.Text = sequence.ToString();
    sequence++; //increase the sequence 
value by one every time this event is called
}

//It is trapping prerender event page
void Page_PreRender(Object o,EventArgs e)
{
    lblPagePreRender.Text = sequence.ToString();
    sequence++; //increase the sequence 
value by one every time this event is called
}

//It is trapping unload event page
void Page_Unload (Object sender , EventArgs e)
{
    lblPageUnload.Text = sequence.ToString();
    sequence++; //increase the sequence 
value by one every time this event is called
}

</script>

<html>
<head>
<style>
       .title 
{font-family:verdana;font-size:12pt;font-weight:bold;}
       .subtitle 
{font-family:verdana;font-size:10pt;font-weight:bold;font-style:italic;}
       .sequence 
{font-family:verdana;font-size:10pt;font-weight:normal;}
</style>
</head>
<body>
<form Runat="server">
<table border="0" width="500" cellpadding="1" cellspacing="1">
<tr>
    <td width="70%" height="30" align="center" bgColor="orange">
           <span class="title">Page Events</span>
    </td>
    <td width="30%" height="30" align="center" bgColor="orange">
           <span class="title">Sequence No.</span>
    </td>
</tr>
<tr>
    <td>
          <span class="subtitle">On Load</span>
    </td>
    <td align="center">
          <asp:Label id="lblPageLoad" Runat="server" class="sequence"/>
    </td>
</tr>
<tr>
    <td bgColor="#d9d9d9">
          <span class="subtitle

     
">OnInit</span></td><tdbgColor="#d9d9d9"align="center">
          <asp:Label id="lblPageInit" Runat="server" class="sequence"/>
    </td>
</tr>
<tr>
    <td>
          <span class="subtitle">On 
PreRender</span>
    </td>
    <td align="center">
          <asp:Label id="lblPagePreRender" 
Runat="server" 
class="sequence"/>
    </td>
</tr>
<tr>
    <td bgColor="#d9d9d9">
          <span class="subtitle">OnUnLoad</span>
    </td>
    <tdbgColor="#d9d9d9"align="center">
    <asp:Label id="lblPageUnload" Runat="server" class="sequence"/>
    </td>
</tr>
</table>
</form>
</body>
</html>

Points of Interest

By knowing which event is called first or next, it will give a new ASP.NET programmer an idea about where to keep a code as intended. Example: if they want you to do specific things when the web page is being loaded, you just need to keep your code inside Page_Load.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Gunawan Hadikusumo
Web Developer
Australia Australia
Born in Jakarta,Indonesia. Now he is in New Zealand and currently working for Macvad Limited.
 
"Programming is about practice and practice, then try to bring the best programming practice into the real world application / web development."

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberrameshsahu78-Jul-09 23:38 
Questionto call page_UnLoad event server side in asp.cs Pinmemberhafz18-Apr-06 1:09 
AnswerRe: to call page_UnLoad event server side in asp.cs Pinmembersgorbutica17-Sep-06 23:52 
AnswerRe: to call page_UnLoad event server side in asp.cs PinmemberSpiff Dog5-Oct-06 10:04 
Questionhelp! what's wrong with my code? PinmemberYour details have been updated28-Aug-05 21:31 
GeneralHi Gun Pinmembercharjo17-May-05 10:53 
GeneralGood and Tidy Example for beginner PinsussVeronika Steffanca26-Jan-05 7:59 
GeneralAlready been documented PinmemberJacob Slusser25-Jan-05 5:15 
GeneralRe: Already been documented PinmemberCaelib28-Jan-05 3:32 
GeneralRe: Already been documented PinsussAnonymous31-Jan-05 7:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 25 Jan 2005
Article Copyright 2005 by Gunawan Hadikusumo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid