Exploring the Page Class                                                
               
All ASPX Web pages inherit from the System.Web.UI.Page class.























Like all types in .NET, the Page class ultimately inherits from the Object class, which includes the classic members:
ToString(), GetHashCode(), etc. You can also see that the Page class inherits from the Control class.
All Web pages and their controls inherit from the Control class. In other words, the Web page is just another control!

Most of the basic ASP.NET events are defined in the Control class. Since all Web pages and all Web controls derive
from Control, you can write event handlers in both for the same events!

You may also define a custom base class that inherits from Page. You would then redefine all your Web pages (code-
behind files and single-files) to inherit from your custom base class, not the Page class. This enables you to define
“global” properties, methods and events for all your Web pages. Thankfully, this is supported by all three page
architectures and the VS 2008 Web page designer.

The Page class inherits several important members from Control. Here are some that you’ll be using most often with
controls and Web pages:






















































We will discuss Web Controls thoroughly in the next chapter.

The Page class also adds more interesting members that make Web page programming easy.
















































































Note that five of the six classic “Intrinsic” objects of classic ASP (Active Server Pages) programming are exposed
through the Page class. They are: Application, Request, Response, Session, and Server. This eases the pain of Web
page migration from classic ASP to ASP.NET (and Web programmer migration!)

However, the ASP’s ObjectContext object was not migrated to ASP.NET. Instead, you use the System.
EnterpriseServices.ContextUtil object for the same purpose. To use ContextUtil, your Web project must reference
the System.EnterpriseServices.dll assembly.

Using the Page members from the ASPX or the code-behind file is easy. Notice below how you can access members
explicitly or implicitly:


public partial class Default2 : Page
{   // C#
  protected void Page_Load(object sender, EventArgs e)
  { // EXPLICIT
    if (!Page.IsPostBack)
      if (Page.Application["HitCount"] == null)
        Page.Application["HitCount"] = 0;
      else
        Page.Application["HitCount"] = (int)Page.Application["HitCount"]+1;

    // IMPLICIT
    if (!IsPostBack)
      if (Application["HitCount"] == null)
        Application["HitCount"] = 0;
      else
        Application["HitCount"] = (int)Application["HitCount"] + 1;
  }

  protected void Button1_Click(object sender, EventArgs e)
  {  Page.Response.Redirect("Default.aspx");  }

  protected void Button2_Click(object sender, EventArgs e)
  {  Response.Redirect("Default.aspx");  }
}



‘ VB
Partial Class Default3
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.Load

  ‘ EXPLICIT
  If Not Page.IsPostBack Then
    If IsNothing(Page.Application("HitCount")) Then
      Page.Application("HitCount") = 0
    Else
      Page.Application("HitCount") = CInt(Page.Application("HitCount")) + 1
    End If
  End If

  ‘ IMPLICIT
  If Not IsPostBack Then
    If IsNothing(Application("HitCount")) Then
      Application("HitCount") = 0
    Else
      Application("HitCount") = CInt(Application("HitCount")) + 1
    End If
  End If

End Sub

Protected Sub Button1_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Button1.Click
      Page.Response.Redirect("Default.aspx")
End Sub

Protected Sub Button2_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Button2.Click
      Response.Redirect("Default.aspx")
End Sub

End Class


You will now take a closer look at some of the Page members in action.
Page Class
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials

Interesting Members of   
System.Web.UI.Control

                      Meaning in Life

Controls

 This “Get/Set” property is a ControlCollection object
 that represents the containing child controls for a Web
 page or Web control.

EnableViewState

This “Get/Set” Boolean property defines whether a
server control will persist its view state. This also affects
whether the view state of all child controls.

ID

This “Get/Set” property holds the unique identifier for the
server control.

Page

This “Get” property holds a reference to the Page instance
containing the server control.

Parent

This “Get” property is a reference to the server control's
parent (container) control.

Visible

This “Get/Set” property determines whether a server control
should be rendered as HTML on the page.

DataBind()

Binds a data source (DataReader, DataSet, etc.) to the Web
server control and all of its child controls.

HasControls()

 Returns a Boolean value indicating whether a server control
 contains any child controls.

 DataBinding

 This event occurs when the Web server control binds to a data
  source.

 Init

 This event occurs when the Web server control is initialized,
 which is the first step in its lifecycle.
    
  Load

  This event occurs when the server control is loaded into the
  Page object.

 Unload

  This event occurs when the server control is unloaded from
  memory.

Interesting Members of
System.Web.UI.Page

                                          Meaning in Life

Application

 Gets the System.Web.HttpApplicationState object for the current
Web request. This is used to store variables across all users’ sessions.

 Cache

 Gets the System.Web.Caching.Cache object associated with the
application in which the page resides. Behaves like Application
variables but throws them away when memory is low on the Web server.

 ClientScript

 Gets a System.Web.UI.ClientScriptManager object used to manage,
 register, and add scripts to the page.

 ErrorPage

Gets or sets the error page to which the requesting browser is redirected
in the event of an unhandled page exception.

 Form

 Gets the HTML Form on the page. Only one form is allowed.

 IsCrossPagePostBack

 Gets a value indicating whether the page is involved in a cross-page
 postback.

 IsPostBack

 Gets a Boolean that states whether the page is posting back to itself or
if it’s being loaded for the first time. Often used in the Page_Load event
handler.

 Request

 Gets the System.Web.HttpRequest object for the requested page.
 Exposes the incoming request and all its properties.

 Response

 Gets the System.Web.HttpResponse object associated with the
 System.Web.UI.Page object. Exposes the outgoing response and all its
properties.

 Server

 Gets the Server object, which is an instance of the
 System.Web.HttpServerUtility class. Exposes many server variables
and useful string-manipulation methods.

 Session

 Gets the current Session object provided by ASP.NET. This is used to
 store variables for a particular user’s session.

 EncryptString()

 Static method that encrypts passed-in string data using the unique Web
 server key in machine.config.

DecryptString()

 Static method that decrypts passed-in string data using the unique Web
server key in machine.config.

 FindControl()

Searches the page container for a server control with the specified
identifier.

 MapPath()

Retrieves the physical path that a virtual path, either absolute or
relative, or an application-relative path maps to.

 
RegisterClientScriptBlock()

Emits client-side script blocks to the response.

SetFocus()

Sets the browser focus to the control with the specified identifier.

Validate()

Instructs any validation controls included on the page to validate their
assigned information.

OnPreInit

This event is raised at the beginning of page initialization. Here is
where you can programmatically set the Master page or Theme.
Services