Events on Web Page






1.46/5 (8 votes)
Jan 25, 2005

170133

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