System.Web.UI


.NET Framework 类库
Page 类

表示从 ASP.NET Web 应用程序的宿主服务器请求的 .aspx 文件(又称为 Web 窗体页)。

命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

语法

Visual Basic(声明)
Public Class Page
    Inherits TemplateControl
    Implements IHttpHandler
Visual Basic(用法)
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
备注

Page 类与扩展名为 .aspx 的文件相关联。这些文件在运行时被编译为 Page 对象,并被缓存在服务器内存中。

如果要使用代码隐藏技术创建 Web 窗体页,请从该类派生。应用程序快速开发 (RAD) 设计器(如 Microsoft Visual Studio)自动使用此模型创建 Web 窗体页。

Page 对象充当页中所有服务器控件(实现 INamingContainer 接口的控件或实现此接口的控件的子控件除外)的命名容器。

Page 类是一个用作 Web 应用程序的用户界面的控件,因此应对这样的类进行仔细检查以确保遵循编写安全代码和保证应用程序安全的最佳做法。有关这些主题的常规信息,请参见 Web 应用程序安全威胁概述安全策略最佳实施策略安全性的基础概念。有关更具体的信息,请参见 保证标准控件的安全如何:显示安全错误信息如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入验证控件介绍

TopicLocation
如何:确定调用 ASP.NET 网页的方式生成 ASP .NET Web 应用程序
如何:确定调用 ASP.NET 网页的方式在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:通过遍历控件集合定位页上的 Web 窗体控件生成 ASP .NET Web 应用程序
如何:通过遍历控件集合定位页上的 Web 窗体控件在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:为移动设备创建网页在 Visual Studio 中构建 ASP .NET Web 应用程序
示例

下面的代码示例演示如何在代码隐藏页模型中使用 Page 类。注意,代码隐藏源文件声明了一个从基页类继承的分部类。基页类可以是 Page,也可以是从 Page 派生的其他类。另外请注意,分部类允许代码隐藏文件使用页中定义的控件,而无需将其定义为字段成员。

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 + ".";

    }
}

下面的代码示例演示与前面的代码隐藏源文件对应的 .aspx 文件。

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>

必须使用 @ Page 指令并使用 InheritsCodeFile 属性将代码隐藏文件链接至 .aspx 文件。在此示例中,Inherits 属性指示 MyCodeBehind 类,CodeFile 属性指示包含该类的语言特定的文件的路径。

下面的代码示例演示单文件页模型以及如何访问 PageIsPostBack 属性和 Response 属性。

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 安全性

继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.TemplateControl
      System.Web.UI.Page
         System.Web.UI.MobileControls.MobilePage
线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台

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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0
请参见

标记 :


Page view tracker