System.Web.UI


.NET Framework Class Library
Page Class

Represents an .aspx file, also known as a Web Forms page, requested from a server that hosts an ASP.NET Web application.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

Syntax

Visual Basic (Declaration)
Public Class Page
    Inherits TemplateControl
    Implements IHttpHandler
Visual Basic (Usage)
Dim instance As Page
C#
public class Page : TemplateControl, IHttpHandler
C++
public ref class Page : public TemplateControl, IHttpHandler
J#
public class Page extends TemplateControl implements IHttpHandler
JScript
public class Page extends TemplateControl implements IHttpHandler
Remarks

The Page class is associated with files that have an .aspx extension. These files are compiled at run time as Page objects and cached in server memory.

If you want to create a Web Forms page using the code-behind technique, derive from this class. Rapid application development (RAD) designers, such as Microsoft Visual Studio, automatically use this model to create Web Forms pages.

The Page object serves as the naming container for all server controls in a page, except those that implement the INamingContainer interface or are child controls of controls that implement this interface.

The Page class is a control that acts as the user interface for your Web application, and as such should be scrutinized to make sure best practices for writing secure code and securing applications are followed. For general information on these topics, see Overview of Web Application Security Threats, Security Policy Best Practices, and Key Security Concepts. For more specific information, see Securing Standard Controls, How to: Display Safe Error Messages, How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Introduction to the Validation Controls.

TopicLocation
How to: Access Controls by using the Controls CollectionBuilding ASP .NET Web Applications in Visual Studio
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications in Visual Studio
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications in Visual Studio
How to: Locate the Web Forms Controls on a Page by Walking the Controls CollectionBuilding ASP .NET Web Applications
How to: Locate the Web Forms Controls on a Page by Walking the Controls CollectionBuilding ASP .NET Web Applications
How to: Locate the Web Forms Controls on a Page by Walking the Controls CollectionBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating Web Pages for Mobile DevicesBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating Web Pages for Mobile DevicesBuilding ASP .NET Web Applications in Visual Studio
Example

The following code example demonstrates how the Page class is used in the code-behind page model. Note that the code-behind source file declares a partial class that inherits from a base page class. The base page class can be Page, or it can be another class that derives from Page. Furthermore, note that the partial class allows the code-behind file to use controls defined on the page without the need to define them as field members.

Visual Basic
Imports System

Partial Class MyCodeBehindVB
    Inherits System.Web.UI.Page


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

        ' Place page-specific code here.

    End Sub

    ' Define a handler for the button click.
    Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click

        MySpan.InnerHtml = "Hello, " + MyTextBox.Text + "."

    End Sub

End Class
C#
using System;

public partial class MyCodeBehindCS : System.Web.UI.Page
{     
    protected void Page_Load(object sender, EventArgs e)
    {

        // Place page-specific code here.


    }

    // Define a handler for the button click.
    protected void SubmitBtn_Click(object sender, EventArgs e)
    {    

        MySpan.InnerHtml = "Hello, " + MyTextBox.Text + ".";

    }
}

The following code example shows the .aspx file that corresponds to the preceding code-behind source file.

Visual Basic
<%@ Page Language="VB" CodeFile="pageexample.aspx.vb" Inherits="MyCodeBehindVB" %>

<html>
<head runat="server">
    <title>Page Class Example</title>
</head>
<body>
    <form runat="server">
    <div>
       <table>
          <tr>
            <td> Name: </td>
            <td> <asp:textbox id="MyTextBox" runat="server"/> </td>
          </tr>
          <tr>
             <td></td>
             <td><asp:button id="MyButton" text="Click Here" onclick="SubmitBtn_Click" runat="server"/></td>
          </tr>
          <tr>
             <td></td>
             <td><span id="MySpan" runat="server" /></td>
          </tr>
       </table>         
    </div>
    </form>
</body>
</html>
C#
<%@ Page Language="C#" CodeFile="pageexample.aspx.cs" Inherits="MyCodeBehindCS" %>

<html>
<head runat="server">
    <title>Page Class Example</title>
</head>
<body>
    <form runat="server">
    <div>
       <table>
          <tr>
            <td> Name: </td>
            <td> <asp:textbox id="MyTextBox" runat="server"/> </td>
          </tr>
          <tr>
             <td></td>
             <td><asp:button id="MyButton" text="Click Here" onclick="SubmitBtn_Click" runat="server"/></td>
          </tr>
          <tr>
             <td></td>
             <td><span id="MySpan" runat="server" /></td>
          </tr>
       </table>     
    </div>
    </form>
</body>
</html>

You must use the @ Page directive and use the Inherits and CodeFile attributes to link the code-behind file to the .aspx file. In this example, the Inherits attribute indicates the MyCodeBehind class and the CodeFile attribute indicates the path to the language-specific file that contains the class.

The following code example demonstrates the single-file page model and how to access the IsPostBack property and the Response property of the Page.

Visual Basic
<%@ Page Language="VB" %>

<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim sb As New StringBuilder()
    
    If (Page.IsPostBack) Then
      
      sb.Append("You posted back to the page.<br>")
    
    End If
    
    sb.Append("The host address is " + Page.Request.UserHostAddress + ".<br>")
    sb.Append("The page title is """ + Page.Header.Title + """.")
    
    PageMessage.Text = sb.ToString()
    
  End Sub
  
</script>

<html>
<head runat="server">
    <title>Page Class Example</title>
</head>
<body>
    <form id="form1" 
          runat="server">
    <div>
    <asp:Label id="PageMessage" 
               runat="server"/>
    <br /> <br />
    <asp:Button id="PageButton"
                Text="PostBack"
                runat="server" />    
    </div>
    </form>
</body>
</html>
C#
<%@ Page Language="C#" %>

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    StringBuilder sb = new StringBuilder();
    
    if (Page.IsPostBack)
      sb.Append("You posted back to the page.<br>");

    sb.Append("The host address is " + Page.Request.UserHostAddress + ".<br>");
    sb.Append("The page title is \"" + Page.Header.Title + "\".");

    PageMessage.Text = sb.ToString();

  }
</script>

<html>
<head runat="server">
    <title>Page Class Example</title>
</head>
<body>
    <form id="form1" 
          runat="server">
    <div>
    <asp:Label id="PageMessage" 
               runat="server"/>
    <br /> <br />
    <asp:Button id="PageButton"
                Text="PostBack"
                runat="server" />
    </div>
    </form>
</body>
</html>
.NET Framework Security

Inheritance Hierarchy

System.Object
   System.Web.UI.Control
     System.Web.UI.TemplateControl
      System.Web.UI.Page
         System.Web.UI.MobileControls.MobilePage
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0
See Also

Tags :


Page view tracker