Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 3. | 2

Build Your Own ASP.NET Website Using C# and VB.NET. Pt. 3.

Until now, we’ve considered only events that are raised by controls. However, there is another type of event—the page event. The idea is the same as for control events[3], except that here, it is the page as a whole that generates the events. You’ve already used one of these events: the Page_Load event. This event is fired when the page loads for the first time. Note that we don’t need to associate handlers for page events the way we did for control events; instead, we just place our handler code inside a subroutine with a preset name. The following list outlines the page event subroutines that are available:

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 the Page_Load event is through an example:

Example 3.4. PageEvents.aspx (excerpt)

<html>
<head>
<script runat="server" language="VB">
Sub Page_Load(s As Object, e As EventArgs)
  lblMessage.Text = "Hello World"
End Sub
</script>
</head>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>

Example 3.5. PageEvents.aspx (excerpt)

<html>
<head>
<script runat="server" language="C#">
void Page_Load(Object s, EventArgs e) {
  lblMessage.Text = "Hello World";
}
</script>
</head>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server" />
</form>
</body>
</html>

You can see that the control on the page does not specify any event handlers. There’s no need, because we’re using the special Page_Load subroutine, which will be called when the page loads. As the page loads, it will call the Page_Load routine, to display “Hello World” in the Label control, as shown in Figure 3.1.

Figure 3.1. The Page_Load event is raised, the subroutine is called, and the code within the subroutine is executed.

The Page_Load event is raised, the subroutine is called, and the code within the subroutine is executed.

Variables are fundamental to programming, and you’ve almost certainly come across the term before. Basically, they let you give a name, or identifier, to a piece of data; we can then use that identifier to store, modify, and retrieve the data.

However, there are, of course, many different kinds of data, such as strings, integers (whole numbers), and floating point numbers (fractions or decimals). Before you can use a variable in VB.NET or C#, you must specify what type of data it can contain, using keywords such as String, Integer, Decimal, and so on, like this:

Dim strName As String
Dim intAge As Integer
string strName;
int intAge;

These lines declare what type of data we want our variables to store, and are therefore known as variable declarations. In VB.NET, we use the keyword Dim, which stands for “dimension”, while in C#, we simply precede the variable name with the appropriate data type.

Sometimes, we want to set an initial value for variables that we declare; we can do this using a process known as initialization:

Dim strCarType As String = "BMW"
string strCarType = "BMW";

We can also declare and/or initialize a group of variables of the same type all at once:

Dim strCarType As String, strCarColor = "blue", strCarModel
string strCarType, strCarColor = "blue", strCarModel;

Table 3.1 below lists the most useful data types available in VB.NET and C#.

There are many more data types that you may encounter as you progress, but this list provides an idea of the ones you’ll use most often.

So, to sum up, once you’ve declared a variable as a given type, it can only hold data of that type. You can’t put a string into an integer variable, for instance. However, there are frequently times when you’ll need to convert one data type to another. Have a look at this code:

Dim intX As Integer
Dim strY As String = "35"
intX = strY + 6
int intX;
String strY = "35";
intX = strY + 6;

Now, while you or I might think that this could make sense—after all, the string strY does contain a number, so we may well wish to add it to another number—the computer will not be happy, and we’ll get an error. What we have to do is explicitly convert, or cast, the string into an integer first:

Dim intX As Integer
Dim strY As String = "35"
intX = Int32.Parse(strY) + 6
int intX;
String strY = "35";
intX = Convert.ToInt32(strY) + 6;

Now, the computer will be happy, as we’ve told it that we want to turn the string into an integer before it’s used as one. This same principle holds true when mixing other types in a single expression.

Arrays are a special variety of variable tailored for storing related items of the same data type. Any one item in an array can be accessed using the array’s name, followed by that item’s position in the array (its offset). Let’s create a sample page to show what I mean:

There are some important points to pick up from this code. First, notice how we declare an array. In VB.NET, it looks like a regular declaration for a string, except that the number of items we want the array to contain is given in brackets after the name:

In C#, it’s a little different. First, we declare that drinkList is an array by following the datatype with two empty square brackets. We must then specify that this is an array of four items, using the new keyword:

A crucial point to realize here is that the arrays in both C# and VB.NET are what are known as zero-based arrays. This means that the first item actually has position 0, the second has position 1, and so on, through to the last item, which will have a position that’s one less than the size of the array (3, in this case). So, we specify each item in our array like this:

Notice that C# uses square brackets for arrays, while VB.NET uses standard parentheses. We have to remember that arrays are zero-based when we set the label text to the second item, as shown here:

To help this sink in, you might like to try changing this code to show the third item in the list instead of the second. Can you work out what change you’d need to make?

That’s right—you only need to change the number given in the brackets to match the position of the new item (don’t forget to start at zero). In fact, it’s this ability to select one item from a list using only its numerical location that makes arrays so useful in programming, as we’ll see as we get further into the book.

Created: March 27, 2003
Revised: July 5, 2004

URL: http://webreference.com/programming/asp_net3/1