Click here to Skip to main content
Licence 
First Posted 24 Jan 2005
Views 83,108
Bookmarked 25 times

Events on Web Page

By | 24 Jan 2005 | Article
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

About the Author

Gunawan Hadikusumo

Web Developer

Australia Australia

Member

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."

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberrameshsahu723:38 8 Jul '09  
Questionto call page_UnLoad event server side in asp.cs Pinmemberhafz1:09 18 Apr '06  
AnswerRe: to call page_UnLoad event server side in asp.cs Pinmembersgorbutica23:52 17 Sep '06  
AnswerRe: to call page_UnLoad event server side in asp.cs PinmemberSpiff Dog10:04 5 Oct '06  
Questionhelp! what's wrong with my code? PinmemberYour details have been updated21:31 28 Aug '05  
GeneralHi Gun Pinmembercharjo10:53 17 May '05  
GeneralGood and Tidy Example for beginner PinsussVeronika Steffanca7:59 26 Jan '05  
GeneralAlready been documented PinmemberJacob Slusser5:15 25 Jan '05  
GeneralRe: Already been documented PinmemberCaelib3:32 28 Jan '05  
GeneralRe: Already been documented PinsussAnonymous7:23 31 Jan '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 25 Jan 2005
Article Copyright 2005 by Gunawan Hadikusumo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid