.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.

For a list of all members of this type, see Page Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.TemplateControl
         System.Web.UI.Page

[Visual Basic]
Public Class Page
   Inherits TemplateControl
   Implements IHttpHandler
[C#]
public class Page : TemplateControl, IHttpHandler
[C++]
public __gc class Page : public TemplateControl, IHttpHandler
[JScript]
public class Page extends TemplateControl implements IHttpHandler

Thread Safety

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

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 Visual Studio .NET, 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 children of controls that implement this interface.

Example

The following example demonstrates extending the Page class to create a code-behind ASP.NET page. When you create a page this way, you must include the System and System.Web.UI namespaces, along with any other namespaces that contain the classes that your page uses. This example accesses the System.Web.UI.WebControls and System.Web.UI.HtmlControls namespaces.

[Visual Basic] 
Option Strict Off

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Public Class MyCodeBehind : Inherits Page

    Public Name As TextBox
    Public MySpan As HtmlGenericControl

    Public Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

        MySpan.InnerHtml = "Hello, " & Name.Text & "."
        
    End Sub

End Class

[C#] 
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class MyCodeBehind : Page {

    public TextBox          Name;
    public HtmlGenericControl   MySpan;

    public void SubmitBtn_Click(Object sender, EventArgs e) {

           MySpan.InnerHtml = "Hello, " + Name.Text + "."; 
  }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;
using namespace System::Web::UI::HtmlControls;

public __gc class MyCodeBehind : public Page 
{
public:
   TextBox * Name;
   HtmlGenericControl * MySpan;

   void SubmitBtn_Click(Object* sender, EventArgs* e) 
   {
      MySpan->InnerHtml = String::Concat(S"Hello, {0}.", Name->Text);
   }
};

[JScript] 
import System
import System.Web.UI
import System.Web.UI.WebControls
import System.Web.UI.HtmlControls

public class MyCodeBehind extends Page {

    public var Name : TextBox;
    public var MySpan : HtmlGenericControl;

    public function SubmitBtn_Click(sender, e : EventArgs) {

           MySpan.InnerHtml = "Hello, " + Name.Text + "."; 
  }
}

In addition to extending the Page class, you must create an .aspx file to visually display the UI associated with the classes you have invoked in your source file. The following example demonstrates an .aspx file that links to the MyCodeBehind class created in the previous example. You must use the @ Page directive and use the Inherits and Src attributes to link the code-behind file to the .aspx file. In this case, the Inherits attribute indicates the MyCodeBehind class and the Src attribute indicates the path to the language-specific file that contains the class.

CAUTION   RAD designers, such as Visual Studio .NET, create ASP.NET pages using the code-behind method automatically. Designers do not use the Src attribute, however.
[Visual Basic] 
<%@ Page Inherits="MyCodeBehind" Src="PageExample.vb" %>
<html>
   
   <body>

       <center>

       <form runat="server">

           

           <table>
              <tr>
                <td> Name: </td>
                <td> <asp:textbox id="Name" 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>

           

       </form>

       </center>

   </body>

</html>

[C#] 
<%@ Page Inherits="MyCodeBehind" Src="PageExample.cs" %>
<html>
   
   <body>

       <center>

       <form runat="server">

           

           <table>
              <tr>
                <td> Name: </td>
                <td> <asp:textbox id="Name" 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>

           

       </form>

       </center>

   </body>

</html>

[JScript] 
<%@ Page Inherits="MyCodeBehind" Src="PageExample.js" %>
<html>
   
   <body>

       <center>

       <form runat="server">

           

           <table>
              <tr>
                <td> Name: </td>
                <td> <asp:textbox id="Name" 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>

           

       </form>

       </center>

   </body>

</html>

Requirements

Namespace: System.Web.UI

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

Page Members | System.Web.UI Namespace | Control | TemplateControl | UserControl | @ Page