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.3  Relationship Between an ASPX File and a Code-Behind File
How are the ASPX and code-behind files used to create the Web page that is sent to the client? First, recall that class WebTime is the base class specified in line 3 of the ASPX file (Fig. 21.4). This class (partially declared in the code-behind file) inherits from Page, which defines the general functionality of a Web page. Partial class WebTime inherits this functionality and defines some of its own (i.e., displaying the current time). The code-behind file contains the code to display the time, whereas the ASPX file contains the code to define the GUI.
When a client requests an ASPX file, ASP.NET creates two classes behind the scenes. Recall that the code-behind file contains a partial class named WebTime. The first file ASP.NET generates is another partial class containing the remainder of class WebTime, based on the markup in the ASPX file. For example, WebTime.aspx contains a Label Web control with ID timeLabel, so the generated partial class would contain a declaration for a Label variable named timeLabel. This partial class might look like
public partial class WebTime{protected System.Web.UI.WebControls.Label timeLabel;}
Note that a Label is a Web control defined in namespace System.Web.UI.WebControls, which contains Web controls for designing a page's user interface. Web controls in this namespace derive from class WebControl. When compiled, the preceding partial class declaration containing Web control declarations combines with the code-behind file's partial class declaration to form the complete WebTime class. This explains why line 19 in method Page_Init of WebTime.aspx.cs (Fig. 21.5) can access timeLabel, which is created in lines 18-20 of WebTime.aspx (Fig. 21.4)-method Page_Init and control timeLabel are actually members of the same class, but defined in separate partial classes.
The second class generated by ASP.NET is based on the ASPX file that defines the page's visual representation. This new class inherits from class WebTime, which defines the page's logic. The first time the Web page is requested, this class is compiled, and an instance is created. This instance represents our page-it creates the XHTML that is sent to the client. The assembly created from our compiled classes is placed within a subdirectory of
C:\WINDOWS\Microsoft.NET\Framework\VersionNumber\Temporary ASP.NET Files\WebTime
where VersionNumber is the version number of the .NET Framework (e.g., v2.0.50215) installed on your computer.
Performance Tip 21.1
 21.1 Once an instance of the Web page has been created, multiple clients can use it to access the page-no recompilation is necessary. The project will be recompiled only when you modify the application; changes are detected by the runtime environment, and the project is recompiled to reflect the altered content.
(Continue to "How the Code in an ASP.NET Web Page Executes".)

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