Click here to Skip to main content
15,868,141 members
Articles / Web Development / ASP.NET
Article

Events on Web Page

Rate me:
Please Sign up or sign in to vote.
1.46/5 (8 votes)
24 Jan 2005 169.1K   646   25   10
Detecting web page events' sequence.

Image 1

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:

ASP.NET
<%@ 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


Written By
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 Pin
RMShoo8-Jul-09 23:38
RMShoo8-Jul-09 23:38 
Questionto call page_UnLoad event server side in asp.cs Pin
hafz18-Apr-06 1:09
hafz18-Apr-06 1:09 
AnswerRe: to call page_UnLoad event server side in asp.cs Pin
sgorbutica17-Sep-06 23:52
sgorbutica17-Sep-06 23:52 
AnswerRe: to call page_UnLoad event server side in asp.cs Pin
Spiff Dog5-Oct-06 10:04
Spiff Dog5-Oct-06 10:04 
Questionhelp! what's wrong with my code? Pin
Your details have been updated28-Aug-05 21:31
Your details have been updated28-Aug-05 21:31 
GeneralHi Gun Pin
charjo17-May-05 10:53
charjo17-May-05 10:53 
GeneralGood and Tidy Example for beginner Pin
Veronika Steffanca26-Jan-05 7:59
sussVeronika Steffanca26-Jan-05 7:59 
GeneralAlready been documented Pin
Jacob Slusser25-Jan-05 5:15
Jacob Slusser25-Jan-05 5:15 
GeneralRe: Already been documented Pin
scott.simmons28-Jan-05 3:32
scott.simmons28-Jan-05 3:32 
GeneralRe: Already been documented Pin
Anonymous31-Jan-05 7:23
Anonymous31-Jan-05 7:23 
I just did a quick glance at the other articles you linked (and the MSDN one), and IMO - they can go a bit overkill on information, epsically if you are just a beginner and want a very quick idea of - which of these get fired in what order?

While once you know that, and may want to dig deeper into things, the other articles are more usefull, but for what this one is trying to offer, it does it quite well and I would of found it to be more usefull for my needs a few months ago than the other articles you linked.

Information-Overload of being 'comphrensive' can prevent you from being able to see the forest because of the trees'.

While it may not be the most indepth article on the topic, I think it does what it aimed to do quite nicely.

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

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