Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online
Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett
Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework
As with OnClick, the subroutine indicated by this attribute is called when the
button is clicked.
OnLoad
The subroutine indicated by this attribute is called when the button is loaded
for the first time—usually when the page first loads.
OnInit
When the button is initialized, any subroutine given in this attribute will be
called.
OnPreRender
We can use this attribute to run code just before the button is rendered.
OnDisposed
The subroutine specified by this attribute is executed when the button is released
from memory.
Licensed to [email protected]
50
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
OnDataBinding
This attribute fires when the button is bound to a data source.
Don’t worry too much about the details of all these events and when they occur; I
just want you to understand that a single control can produce a number of different
events. In the case of the Button control, you’ll almost always be interested in the
Click event; the others are only useful in rather obscure circumstances.
When a control raises an event, the specified subroutine (if one is specified) is executed. Let’s take a look at the structure of a typical subroutine that interacts with a web control:
Visual Basic
Public Sub mySubName(s As Object, e As EventArgs)
⋮
subroutine code…
End Sub
C#
public void mySubName(Object s, EventArgs e)
{
⋮
subroutine code…
}
Let’s take a moment to break down all the components that make up a typical subroutine:
Public (Visual Basic)
public (C#)
This keyword defines the level of visibility the subroutine has in relation to the
rest of the page. There are a few different options to choose from, the most frequently used being Public (for a global subroutine that can be used anywhere within the entire page) and Private (for subroutines that are available for the
specific class only). We’ll analyze these options in more detail a bit later in the
chapter.
Licensed to [email protected]
VB and C# Programming Basics
51
Sub (Visual Basic)
void (C#)
This keyword defines the chunk of code as a subroutine. A subroutine is a
named block of code that doesn’t return a result; thus, in C#, we use the void
keyword, which means exactly what the name says. We don’t need this in VB,
though, because the Sub keyword implies that no value is returned.
mySubName(…)
This part gives the name we’ve chosen for the subroutine. The parameters and
their data types are mentioned in the parentheses.
s As Object (Visual Basic)
Object s (C#)
When we write a subroutine that will function as an event handler, it must accept
two parameters. The first is a reference to the control that fired the event. Each
control has a particular type, such as Label or TextBox, but Object is a generic
type that can be used to reference any kind of object in .NET—even basic types,
such as numbers or strings. Here, we’re putting that Object in a variable named
s (again, we’ll talk more about variables later in 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 that’s specific to the
event that was raised. Note that, in many cases, you won’t need to use either of
these two parameters, so you don’t need to worry about them too much at this
stage.
As this chapter progresses, you’ll 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, we’ve considered only events that are raised by controls. However, there
is another type of event: the
page event
. Technically, a page is simply another type
of control, so its events follow the same principles as those of controls.
Licensed to [email protected]
52
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
The idea is the same as for control events, except that here, it’s the page as a whole
that generates the events.1 You’ve already used one of these events: the Page_Load
event, which is fired when the page loads for the first time. Note that we don’t 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
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 they’re
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 your
LearningASP
directory:
Visual Basic
LearningASP\VB\PageEvents.aspx
<%@ Page Language="VB" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1 Strictly speaking, a page is simply another type of control, so page events
are
actually control events. But when you’re first learning ASP.NET, it can be helpful to think of page events as being different, especially since you don’t usually use On
EventName
attributes to assign subroutines to handle them. Licensed to [email protected]
VB and C# Programming Basics
53
C#
LearningASP\CS\PageEvents.aspx
(excerpt)
<%@ Page Language="C#" %>
⋮
⋮
You can see that the event handlers (the functions that are executed to handle the
events) aren’t specifically defined anywhere. There’s 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 we’ve used in the code (Page_Init, Page_Load, and so on).
As the page loads, it will generate a number of events. We’ve added a text message
to the Label control within each event’s event handler; 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 doesn’t generate any output. Why not? At that point,
the HTML output has already been generated and sent to the browser.
Popular Page_Load
The event you’ll make the most use of in your code is Page_Load. However, in
certain situations the other events will be helpful as well. It’s also worth noting
that ASP.NET supports other events, which we haven’t covered here. You’ll only
need those when it comes to certain complex applications that aren’t within the
scope of this book.
Variables and Variable Declaration
Variables
are fundamental to programming, and you’re almost certain to have come
across the term before. Basically, variables let you give a name, or identifier, to a
specific piece of data; we can then use that identifier to store, modify, and retrieve
the data in question.
VB and C# have access to the same basic data types, which are defined as foundation
classes of the .NET Framework. However, they can be named differently, as each
language defines its own aliases. There are many different kinds of data types, including strings, integers (whole numbers), and floating point numbers (fractions or Licensed to [email protected]
VB and C# Programming Basics
55
Figure 3.2. Handling ASP.NET events
decimals). Before you can use a variable in VB or C#, you must specify the types of
data it can contain using keywords such as String, Integer, and Decimal, like this:
Visual Basic
Dim name As String
Dim age As Integer
C#
string name;
int age;
These lines declare the types of data we want our variables to store, and are therefore
known as
variable declarations
. In VB, we use the keyword Dim, which is short for