Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Visual C# 2005 How to Program, 2/e
Visual C# 2005 How to Program, 2/e

ISBN:
0-13-152523-9
© 2006
pages: 1535

Order
Amazon logo

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 (in an ASPX file a directive is delimited by <%@ and %>) to specify information needed by ASP.NET to process this file. The Language attribute of the Page directive specifies the language of the code-behind file as C#; the code-behind file (i.e., the CodeFile) is WebTime.aspx.cs. Note that a code-behind file name usually consists of the full ASPX file name (e.g., WebTime.aspx) followed by the .cs 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 respectivel. (We discuss these events later in the chapter.)
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 4) to False. We explain the significance of this attribute later in the chapter. 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, unlike C# code, ASP.NET markup is not case-sensitive, so using a different case is not problematic. To remain consistent with the code generated by the IDE, we do not modify these values in our code listings or in our accompanying discussions.]
For this first ASPX file, we provide a brief discussion of the XHTML markup. We do not discuss the majority of the XHTML contained in subsequent ASPX files. 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 the root element html and mark up 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. Notice the runat attribute in line 10, which is set to "server". This attribute indicates that when a client requests this ASPX file, ASP.NET processes the head element and its nested elements 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 executes on the server, which generates equivalent XHTML and sends it to the client. Lines 15-22 contain a div element that groups the elements of the form in 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 in 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
 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 (You are here.)

Tutorial Index