Deitel & Associates, Inc. Logo

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

© 2006
pages: 1535

Amazon logo

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.
Line 13 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 13 of Fig. 21.5 indicates that the code-behind file actually is a partial class. We discuss the remaining portion of this class shortly.
Fig. 21.5 Code-behind file for a page that displays the Web server's time.
1   // Fig. 21.5: WebTime.aspx.cs
2   // Code-behind file for a page that displays the current time.
3   using System;
4   using System.Data;
5   using System.Configuration;
6   using System.Web;
7   using System.Web.Security;
8   using System.Web.UI;
9   using System.Web.UI.WebControls;
10  using System.Web.UI.WebControls.WebParts;
11  using System.Web.UI.HtmlControls;
13  public partial class WebTime : System.Web.UI.Page
14  {
15     // initializes the contents of the page
16     protected void Page_Init( object sender, EventArgs e )
17     {
18        // display the server's current time in timeLabel
19        timeLabel.Text = string.Format( "{0:D2}:{1:D2}:{2:D2}",
20           DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second );
21     } // end method Page_Init
22  } // end class WebTime

Click Here to View Full-Size Image

Line 13 indicates that WebTime inherits from class Page in namespace System.Web.UI. This namespace contains classes and controls that assist in building Web-based applications. Class Page provides event handlers and objects necessary for creating Web-based applications. In addition to class Page (from which all Web applications directly or indirectly inherit), System.Web.UI also includes class Control-the base class that provides common functionality for all Web controls.
Lines 16-21 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 19-20 retrieves the current time and formats it as HH:MM:SS. For example, 9 AM is formatted as 09:00:00, and 2:30 PM is formatted as 14:30:00. Notice 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 (You are here.)

Tutorial Index