DekGenius.com
[ Team LiB ] Previous Section Next Section

Chapter 12. The Page Class

In contrast to classic ASP, ASP.NET features a far richer object model that allows virtually every part of an ASP.NET application to be generated and modified dynamically. Central to this ability to generate—and particularly, to modify—content programmatically is the Page class, which is new to ASP.NET.

The Page class (or a class derived from the Page class) represents a request to an .aspx page that is processed by the ASP.NET extension to the Internet Information Server or to another web server supporting the .NET Framework. The web page may contain simple HTML and text, .NET code, or a combination of both; in other words, the Page class represents a single instance of a Web Forms page. The requests for that page are served by the compiled object that sends HTML or other content back to the client.

The Page object is recompiled if any source files that form this page, such as a user control, a code-behind file, the .aspx page itself, or the application configuration file, are changed.

In the case of single-file ASP.NET pages (i.e., .aspx files that combine user interface elements with script), the .aspx page is compiled into an instance of a class that derives directly from the Page class. This is evident from the following code:

Public Sub Page_Load(o AS Object, e AS EventArgs)
   Dim oType As Type
   oType = Me.GetType
   Do
      Response.Write(oType.Name & "<BR />")
      oType = oType.BaseType
   Loop While Not oType Is Nothing
End Sub

The output produced by this code appears as follows:

Page1_aspx
Page
TemplateControl
Control
Object

Web Forms pages produced by Visual Studio, in contrast, consist of separate .aspx and code-behind files. In this case, the .aspx page is compiled into an instance of a class that derives from the class in the code-behind file, which in turn derives from the Page class. This is illustrated by the following code-behind file:

Option Strict On
  
Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Web
Imports System.Web.UI
  
Namespace AspNetPages
   Public Class Page2Class : Inherits Page
      Public Sub Page_Load(o AS Object, e AS EventArgs) _
                 Handles MyBase.Load
         Dim oType As Type
         oType = Me.GetType
         Do
            Response.Write(oType.Name & "<BR />")
            oType = oType.BaseType
         Loop While Not oType Is Nothing
      End Sub
   End Class
End Namespace

The page produces the following output:

Page2_aspx
Page2Class
Page
TemplateControl
Control
Object

As the output from these two code examples shows, the Page class derives from the System.Web.UI.TemplateControl class, which defines the functionality common to both the Page class and the UserControl class. Such Page class members as the LoadControl method (which dynamically loads a control at runtime), the AbortTransaction and CommitTransaction events, and the Error event are all inherited from TemplateControl. The TemplateControl class derives from the System.Web.UI.Control class, which defines the members common to all ASP.NET Server Controls. The Control class derives from the Object class, the class from which all .NET reference types are derived directly or indirectly.

Because an object derived from the Page class is globally available whenever ASP.NET is processing a Web Forms page, you do not have to reference the Page object specifically to access its members. For example, to access the Session property of the Page class, you can use either:

Dim oSess As HttpSessionState = Page.Session

or:

Dim oSess As HttpSessionState = Session

In addition to representing the Web Form, the Page object is the container for all controls hosted by the page. All child controls on the page can be accessed through the Page object's Controls collection, which returns a ControlCollection object. For example, the following code iterates the ControlCollection collection and lists the name of each control:

Private Sub Page_Load(o As Object, e AS EventArgs)
   Dim ctl As Control
   For each ctl in Controls
      Response.Write(TypeName(ctl) & ": " & ctl.ID & "<BR />")
   Next
End Sub

Table 12-1 lists the properties, collections, and methods exposed by the Page class that are documented in this chapter.

Table 12-1. Page class summary

Properties

Collections

Methods

Events

Application

Controls

DataBind

Error

Cache

Validators

FindControl

Init

ClientTarget

 

HasControls

Load

Context

 

LoadControl

Unload

EnableViewState

 

MapPath

 

ErrorPage

 

ResolveUrl

 

IsPostBack

 

Validate

 

IsValid

   

Request

   

Response

   

Server

   

Session

   

SmartNavigation

   

Trace

   

User

   

ViewState

   

ViewStateUserKey

   

    [ Team LiB ] Previous Section Next Section