Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Visual Basic 2005
How to Program, 3/e

ISBN:
0-13-186900-0
© 2005
pages: ~1500

Pre-order!
Amazon logo

This is the fourth in a series of four tutorials that introduces ASP.NET 2.0 and Microsoft's Visual Web Developer Express IDE for building Web applications. The Visual Web Developer Express functionality we discuss is also part of the complete Visual Studio 2005. Both Visual Web Developer Express and Visual Studio 2005 are scheduled to be released in November 2005. This series of tutorials is a small part of Chapter 21, ASP.NET, Web Forms and Web Conrols, from our forthcoming book Visual Basic 2005 How to Program, 3/e. Chapter 21 is part of a four chapter sequence on XML, ADO.NET, ASP.NET and Web Services in which we discuss each of these technologies and demonstrate how to build substantial, data driven Web applications.

Part 1 provided a brief introduction to ASP.NET, Web Forms and Web controls. Part 2 discussed simple HTTP transactions that enable client/server interactions on the Web. Part 3 overviewed multitier application architecture. This part (which consists of several subsections that you can link to at the bottom of this page) presents a simple Web Form example, analyzes its parts, shows how it executes, and discusses how to build and deploy the Web Form. The tutorials in this series are intended for students and professionals who are already familiar with Visual Basic .NET programming. These tutorials are intended for students and professionals who are already familiar with Visual Basic .NET programming.

[Note: This series of tutorials is an excerpt (Sections 21.1-21.4) of Chapter 21, ASP.NET, Web Forms and Web Controls, from our forthcoming textbook Visual Basic 2005 How to Program, 3/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., Visual Basic 2005 How to Program, ©2005. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

Part 4 Continued: 21.4.2 Examining a Code Behind File
Figure 21.5 presents the code-behind file. Recall that the ASPX file in Fig. 21.4 references this file in line 3.
Fig. 21.5 Code-behind file for a page that displays the Web server's time. 
1   ' Fig. 21.5: WebTime.aspx.vb
2   ' Code-behind file for a page that displays the current time.
3   Partial Class WebTime
4       Inherits System.Web.UI.Page
5   
6       ' initializes the contents of the page
7       Protected Sub Page_Init(ByVal sender As Object, _
8          ByVal e As System.EventArgs) Handles Me.Init
9   
10        ' display the server's current time in timeLabel
11         timeLabel.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", _      
12            DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second)
13      End Sub ' Page_Init
14  End Class ' WebTime

Sample output for the program of Fig. 21.5.

Line 3 begins the declaration of class WebTime. Recall from Chapter 9 that a class declaration can span multiple source-code files and that the separate portions of the class declaration in each file are known as partial classes. The Partial modifier in line 3 of Fig. 21.5 indicates that the code-behind file actually is a partial class. We discuss the remaining portion of this class shortly.
Line 4 indicates that WebTime inherits from class Page in namespace System.Web.UI. This namespace contains classes pertinent to the creation of Web-based applications and controls. Class Page provides event handlers and objects necessary for creating Web-based applications. In addition to the Page class (from which all Web applications directly or indirectly inherit), System.Web.UI also includes the Control class. This is the base class that provides common functionality for all Web controls.
Lines 7-13 define method Page_Init, which handles the page's Init event. This event-the first event raised after a page is requested-indicates that the page is ready to be initialized. The only initialization required for this page is setting timeLabel's Text property to the time on the server (i.e., the computer on which this code executes). The statement in lines 11-12 retrieves the current time and formats it as HH:MM:SS. For example, 9 a.m. is formatted as 09:00:00, and 2:30 p.m is formatted as 14:30:00. Note that the code-behind file can access timeLabel (the ID of the Label in the ASPX file) programmatically, even though the file does not contain a declaration for a variable named timeLabel. You will learn why momentarily. (Continue to "Relationship Between an ASPX File and a Code-Behind File".)

Tutorials in This Series:
ASP.NET Tutorial Part 1: Introduction to ASP.NET
ASP.NET Tutorial Part 2: Simple HTTP Transactions
ASP.NET Tutorial Part 3: Multitier Application Architecture
ASP.NET Tutorial Part 4: Creating and Running a Simple Web Form Example

Tutorial Index