Deitel & Associates, Inc. Logo

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

© 2005
pages: ~1500

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.1 Examining an ASPX File
The ASPX file contains other information in addition to XHTML. Lines 1-2 are ASP.NET comments that indicate the figure number, the file name and the purpose of the file. ASP.NET comments begin with <%-- and terminate with --%>. We added these comments to the file. Lines 3-4 use a Page directive (delimited by <%@ and %>) to specify information needed by the Common Language Runtime (CLR) to process this file. The Language attribute of the Page directive specifies the language of the code-behind file as VB; the code-behind file (i.e., the CodeFile) is WebTime.aspx.vb. Note that a code-behind filename consists of the full ASPX filename (e.g., WebTime.aspx) followed by the .vb extension.
The AutoEventWireup attribute (line 3) determines how Web Form events are handled. When AutoEventWireup is set to true, ASP.NET determines which methods in the class are called in response to an event generated by the Page. For example, ASP.NET will call methods Page_Load and Page_Init in the code-behind file to handle the Page's Load and Init events respectively, without the use of the Handles keyword to explicitly identify the event that each method handles. (We discuss these events later in the chapter.) However, when Visual Web Developer generates an ASPX file, it sets AutoEventWireup to false. This is because any event handlers (such as Page_Load and Page_Init) generated by Visual Web Developer will include the Handles keyword. [Note: The IDE sometimes generates attribute values (e.g., true and false) and control names (as you will see later in the chapter) that do not adhere to our standard code-capitalization conventions (i.e., True and False). However, like Visual Basic code, ASP.NET markup is not case sensitive, so using a different case is not problematic. To remain true to the code generated by the IDE, we do not modify these values in our code listings or in our accompanying discussions.]   
The Inherits attribute (line 4) specifies the class in the code-behind file from which this ASP.NET class inherits-in this case, WebTime. We say more about Inherits momentarily. [Note: We explicitly set the EnableSessionState attribute (line 5) to False. We explain the significance of this attribute later in the chapter.]
For this first ASPX file, we provide a brief discussion of the XHTML markup. We do not discuss most of the XHTML contained in subsequent ASPX files. (For an introduction to XHTML, see Appendix H, Introduction to XHTML: Part 1, and Appendix I, Introduction to XHTML: Part 2, available on the book's CD.) Lines 6-7 contain the document type declaration, which specifies the document element name (HTML) and the PUBLIC Uniform Resource Identifier (URI) for the DTD that defines the XHTML vocabulary.
Lines 9-10 contain the <html> and <head> start tags, respectively. XHTML documents have root element html and markup information about the document in the head element. Also note that the html element specifies the XML namespace of the document using the xmlns attribute (see Section 19.4).
Line 11 sets the title of this Web page. We demonstrate how to set the title through a property in the IDE shortly. Note the runat attribute in line 10, which is set to "server". This attribute indicates that when a client requests this ASPX file over the Web, ASP.NET processes the title element on the server and generates the corresponding XHTML, which is then sent to the client. In this case, the XHTML sent to the client will be identical to the markup in the ASPX file. However, as you will see, ASP.NET can generate complex XHTML markup from simple elements in an ASPX file.
Line 13 contains the <body> start tag, which begins the body of the XHTML document; the body contains the main content that the browser displays. The form that contains our XHTML text and controls is defined in lines 14-23. Again, the runat attribute in the form element indicates that this element will be executed on the server, and equivalent XHTML will be generated and sent to the client. Lines 15-22 contain a div element that groups the elements of the form into a block of markup.
Line 16 is an h2 heading element that contains text indicating the purpose of the Web page. As we demonstrate shortly, the IDE generates this element in response to typing text directly into the Web Form and selecting the text as a second-level heading.
Lines 17-21 contain a p element to mark up content to be displayed as a paragraph in the browser. Lines 18-20 mark up a label Web control. The properties that we set in the Properties window, such as Font-Size and BackColor (i.e., background color), are attributes here. The ID attribute (line 18) assigns a name to the control, so that it can be manipulated programmatically in the code-behind file. We set the control's EnableViewState attribute (line 20) to False. We explain the significance of this attribute later in the chapter.
The asp: tag prefix in the declaration of the Label tag (line 18) indicates that the label is an ASP.NET Web control, not an XHTML element. Each Web control maps to a corresponding XHTML element (or group of elements)-when processing a Web control on the server, ASP.NET generates XHTML markup that will be sent to the client to represent that control in a Web browser.
Portability Tip 21.1
The same Web control can map to different XHTML elements, depending on the client browser and the Web control's property settings.
In this example, the asp:Label control maps to the XHTML span element (i.e., ASP.NET creates a span element to represent this control in the client's Web browser). A span element contains text that is displayed in a Web page. This particular element is used because span elements allow formatting styles to be applied to text. Several of the property values that were applied to our label are represented as part of the style attribute of the span element. You will soon see what the generated span element's markup looks like.
The Web control in this example contains the runat="server" attribute-value pair (line 18), because this control must be processed on the server so that the server can translate the control into XHTML that can be rendered in the client browser. If this attribute pair is not present, the asp:Label element is written as text to the client (i.e., the control is not converted into a span element and does not render properly). (Continue to "Examining 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