this chapter). We can then use that variable to access features and settings
of the specific control from our subroutine.
e As EventArgs (Visual Basic)
EventArgs e (C#)
This, the second parameter, contains certain information thats specific to
the event that was raised. Note that, in many cases, you wont need to use
either of these two parameters, so you dont need to worry about them too
much at this stage.
As this chapter progresses, youll see how subroutines that are associated with
particular events by the appropriate attributes on controls can revolutionize the
way your user interacts with your application.
Page Events
Until now, weve considered only events that are raised by controls. However,
there is another type of event: the page event. Technically, a page is simply an-
other type of control, so page events are a particular kind of control event.
The idea is the same as for control events, except that here, it is the page as a
whole that generates the events.
2
Youve already used one of these events: the
Page_Load event, which is fired when the page loads for the first time. Note that
we dont need to associate handlers for page events as we did for control events;
instead, we just place our handler code inside a subroutine with a preset name.
The following list outlines the most frequently used page event subroutines:
Page_Init
called when the page is about to be initialized with its basic settings
Page_Load
called once the browser request has been processed, and all of the controls
in the page have their updated values
Page_PreRender
called once all objects have reacted to the browser request and any resulting
events, but before any response has been sent to the browser
2
Strictly speaking, a page is simply another type of control, so page events are actually control events.
But when youre first learning ASP.NET, it can be helpful to think of page events as being different,
especially since you dont usually use OnEventName attributes to assign subroutines to handle
them.
56
Chapter 3: VB and C# Programming Basics
Page_UnLoad
called when the page is no longer needed by the server, and is ready to be
discarded
The order in which the events are listed above is also the order in which theyre
executed. In other words, the Page_Init event is the first event raised by the
page, followed by Page_Load, Page_PreRender, and finally Page_UnLoad.
The best way to illustrate how these events work is through an example. Create
the following PageEvents.aspx file in the Learning virtual directory:
Visual Basic File: PageEvents.aspx (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Page Events</title>
<script runat="server" language="VB">
Sub Page_Init(s As Object, e As EventArgs)
messageLabel.Text = "1. Page_Init <br/>"
End Sub
Sub Page_Load(s As Object, e As EventArgs)
messageLabel.Text += "2. Page_Load <br/>"
End Sub
Sub Page_PreRender(s As Object, e As EventArgs)
messageLabel.Text += "3. Page_PreRender <br/>"
End Sub
Sub Page_UnLoad(s As Object, e As EventArgs)
messageLabel.Text += "4. Page_UnLoad <br/>"
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:Label id="messageLabel" runat="server" />
</form>
</body>
</html>
C# File: PageEvents.aspx (excerpt)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Page Events</title>
57
Page Events
<script runat="server" language="C#">
void Page_Init(Object s, EventArgs e)
{
messageLabel.Text = "1. Page_Init <br/>";
}
void Page_Load(Object s, EventArgs e)
{
messageLabel.Text += "2. Page_Load <br/>";
}
void Page_PreRender(Object s, EventArgs e)
{
messageLabel.Text += "3. Page_PreRender <br/>";
}
void Page_UnLoad(Object s, EventArgs e)
{
messageLabel.Text += "4. Page_UnLoad <br/>";
}
</script>
</head>
<body>
<form runat="server">
<asp:Label id="messageLabel" runat="server" />
</form>
</body>
</html>
You can see that the event handlers (the functions that are executed to handle
the events) arent specifically defined anywhere. Theres no need to define them,
because these events are generated by default by the ASP.NET page, and their
handlers have the default names that weve used in the code (Page_Init,
Page_Load, etc). As the page loads, it will generate a number of events. Within
each events event handler, weve added a message to the Label control; this will
give us visual proof that the events actually fire in order. No matter which version
of the code you execute (C# or VB), the output should look like Figure 3.2.
As you can see, Page_UnLoad doesnt generate any output. This is because, at
that point, the HTML output has already been generated and sent to the browser.
Popular Page_Load
The event youll make the most use of in your code is Page_Load. However,
in certain situations the other events will be helpful as well. Its also worth
noting that ASP.NET supports other events, which we havent covered here.
Youll only need those in certain, complex applications that arent in the
scope of this book.
58
Chapter 3: VB and C# Programming Basics

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.