Page クラス
アセンブリ: System.Web (system.web.dll 内)


Page クラスは、.aspx 拡張子を持つファイルと関連付けられています。これらのファイルは、実行時に Page オブジェクトとしてコンパイルされ、サーバー メモリにキャッシュされます。
分離コード技法を使用して Web フォーム ページを作成する場合は、このクラスから派生します。Microsoft Visual Studio などの Rapid Application Development (RAD) デザイナは、自動的にこのモデルを使用して Web フォーム ページを作成します。
Page オブジェクトは、INamingContainer インターフェイスを実装しているコントロール、またはこのインターフェイスを実装しているコントロールの子を除く、ページ内のすべてのサーバー コントロールの名前付けコンテナとして機能します。
Page クラスは、Web アプリケーションのユーザー インターフェイスとして機能するコントロールです。このため、このクラスを使用する際は、安全なコードの作成およびアプリケーションのセキュリティ保護のためのベスト プラクティスに従っていることを注意深く確認する必要があります。これらのトピックの概要については、「Web アプリケーションのセキュリティ上の脅威の概要」、「セキュリティ ポリシーの実施」、および「セキュリティの基本概念」を参照してください。詳細については、「標準コントロールのセキュリティ保護」「方法 : 安全なエラー メッセージを表示する」、「方法 : HTML エンコーディングを文字列に適用して Web アプリケーションをスクリプトによる攻略から保護する」、および「検証コントロールの概要」を参照してください。

Page クラスを分離コード ページ モデルで使用する方法を次のコード例に示します。分離コード ソース ファイルは、基本のページ クラスから継承した部分クラスを宣言します。Page または Page から派生した他のクラスを基本のページ クラスにできます。さらに、部分クラスでは、分離コード ファイルで、フィールド メンバとして定義しなくても、ページ上で定義されたコントロールを使用できます。
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
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 ファイルのコード例を次に示します。
<%@ 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>
<%@ 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 ディレクティブ、および Inherits 属性と CodeFile 属性を使用して、分離コード ファイルを .aspx ファイルと結合する必要があります。この例では、Inherits 属性は MyCodeBehind クラスを示し、CodeFile 属性は、このクラスが使用されている言語固有のファイルへのパスを示します。
シングルファイル ページ モデル、および IsPostBack プロパティと Page の Response プロパティにアクセスする方法を次のコード例に示します。
<%@ 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>
<%@ 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>


System.Web.UI.Control
System.Web.UI.TemplateControl
System.Web.UI.Page
System.Web.UI.MobileControls.MobilePage


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Page クラスのページへのリンク