マスタ ページ [master page]
マスターページ
MasterPage イベント

名前 | 説明 | |
---|---|---|
![]() | AbortTransaction | ユーザーがトランザクションを終了すると発生します。 ( TemplateControl から継承されます。) |
![]() | CommitTransaction | トランザクションが完了すると発生します。 ( TemplateControl から継承されます。) |
![]() | DataBinding | サーバー コントロールがデータ ソースに連結すると発生します。 ( Control から継承されます。) |
![]() | Disposed | サーバー コントロールがメモリから解放されると発生します。これは、ASP.NET ページが要求されている場合のサーバー コントロールの有効期間における最終段階です。 ( Control から継承されます。) |
![]() | Error | 未処理の例外がスローされると発生します。 ( TemplateControl から継承されます。) |
![]() | Init | サーバー コントロールが初期化されると発生します。これは、サーバー コントロールの有効期間における最初の手順です。 ( Control から継承されます。) |
![]() | Load | サーバー コントロールが Page オブジェクトに読み込まれると発生します。 ( Control から継承されます。) |
![]() | PreRender | Control オブジェクトの読み込み後、表示を開始する前に発生します。 ( Control から継承されます。) |
![]() | Unload | サーバー コントロールがメモリからアンロードされると発生します。 ( Control から継承されます。) |

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

Public Class MasterPage Inherits UserControl
public class MasterPage : UserControl
public class MasterPage extends UserControl
public class MasterPage extends UserControl

マスタ ページは、ASP.NET Web アプリケーションのコンテンツ ページのテンプレート コンテナおよびマージ ページとして機能します。マスタ ページを使用すると、一連のコンテンツ ページ全体で構造および内容を簡単に共有できます。コンテンツ プレースホルダを使用して、コンテンツ ページの内容で置き換えるマスタ ページのセクションを定義します。
マスタ ページおよび関連するコンテンツ ページを使用する場合、必要な XHTML 文書タグ (html、head、body など) はマスタ ページのみに追加し、他の .aspx ファイル (ASP.NET ページ) をスタンドアロンの Web ページとして作成しません。コンテンツ ページには、マスタ ページのプレースホルダに挿入する内容を定義します。
実行時に HTTP 要求が行われると、マスタ ページとコンテンツ ページが結合され、コンテンツ ページと同じ名前の単一のクラスが生成されます。結果として得られたコンパイル済みのマージされたクラスは、Page クラスから派生します。
マスタ ページには、直接的なマークアップやサーバー コントロール、およびコンテナ コントロールを定義できます。マスタ ページ内で ContentPlaceHolder コントロールの外側にある各要素は、マスタ ページとコンテンツ ページをマージした結果生成されるすべてのページに表示されます。
マスタ ページに関連する各コンテンツ ページは、@ Page ディレクティブの MasterPageFile 属性でマスタ ページを参照する必要があります。コンテンツ ページには、その @ Page ディレクティブと 1 つ以上の Content コントロールのみを定義できます。ページ テキスト、マークアップ、およびサーバー コントロールはすべて、Content コントロール内に配置する必要があります。Content コントロールが関連付けられているマスタ ページの ContentPlaceHolder コントロールを識別するには、Content コントロールの ContentPlaceHolderID プロパティを設定します。
実行時に、要求されたページの各 Content コントロールの動的な内容がマスタ ページにマージされ、各コントロールの関連する ContentPlaceHolder コントロールの正確な場所に配置されます。マスタ ページ内のその他のマークアップおよびコントロールは影響を受けません。イベント ハンドラは、マスタ クラスにもコンテンツ ページにも定義できます。詳細については、「ASP.NET のマスター ページとコンテンツ ページのイベント」を参照してください。
MasterPage クラスは、.master 拡張子を持つファイルと関連付けられています。これらのファイルは、実行時に MasterPage オブジェクトとしてコンパイルされ、サーバー メモリにキャッシュされます。
コンテンツ ページでマスタ ページを利用できるようにするには、Page 基本クラスの Master プロパティを使用します。Master プロパティはマスタ ページのインスタンスを返しますが、このインスタンスは MasterPage 基本クラスとして型指定されています。マスタ ページのコントロール、プロパティ、および関数にアクセスするために、Master プロパティを MasterPage にキャストできます。マスタ ページのクラス名は、@ Master ディレクティブの ClassName 属性を使用して定義します。
![]() |
---|
マスタ ページで有効なディレクティブは UserControl オブジェクトで使用できるディレクティブと同じであり、次の属性を設定できます。
-
AutoEventWireup
-
ClassName
-
CodeFile
-
CompilerMode
-
CompilerOptions
-
EnableTheming
-
EnableViewState
-
Inherits
-
LinePragmas
-
MasterPageFile
-
WarningLevel
マスタ ページのディレクティブは、個別のコンテンツ ページのディレクティブをオーバーライドしません。
通常、マスタ ページは宣言によって作成されます。マスタ ページをプログラムによって作成する場合は、MasterPage クラスから直接派生させます。MasterPage クラスを拡張することに加えて、.master ファイルを作成して、ソース ファイルで呼び出したクラスに関連付けられているユーザー インターフェイス (UI) を視覚的に表示できるようにする必要があります。
マスタ ページの詳細については、「ASP.NET マスター ページの概要」を参照してください。

宣言によってマスタ ページを作成し、コンテンツ ページを使用してマスタ ページに内容を追加する方法を次のコード例に示します。最初の Web ページは、MasterPageSample_1.master という名前の .master ページです。
<%@ Master Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html > <head runat="server"> <title>MasterPage Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server" /> </div> </form> </body> </html>
<%@ Master Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html > <head runat="server"> <title>MasterPage Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server" /> </div> </form> </body> </html>
MasterPageSample_1.master に関連付けられているコンテンツ ページを表すコード例を次に示します。このコード例には、Content コントロールが含まれており、ContentPlaceHolderID プロパティを使用して、内容が関連付けられている ContentPlaceHolder コントロールを指定しています。
<%@ Page Language="C#" MasterPageFile="~/MasterPageSample_1cs.master" Title="Content Page"%> <asp:content runat="server" contentplaceholderid="ContentPlaceHolder1" >Hello, Master Pages!</asp:content>
<%@ Page Language="VB" MasterPageFile="~/MasterPageSample_1vb.master" Title="Content Page"%> <asp:content runat="server" contentplaceholderid="ContentPlaceHolder1" >Hello, Master Pages!</asp:content>
マスタ ページにプロパティを追加する方法を次のコード例に示します。ClassName 属性を使用して、マスタ ページの名前を指定しています。
<%@ Master Language="C#" ClassName="MasterExample" %> <script runat="server"> public string SiteName { get { return "My Site Name"; } } </script> <html > <head runat="server"> <title>MasterPage Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder> </div> </form> </body> </html>
<%@ Master Language="VB" ClassName="MasterExample" %> <script runat="server"> Public ReadOnly Property SiteName() As String Get Return "My Site Name" End Get End Property </script> <html > <head runat="server"> <title>MasterPage Example</title> </head> <body> <form id="form1" runat="server"> <div> <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> </asp:contentplaceholder> </div> </form> </body> </html>
コンテンツ ページを使用して、前述のコード例のマスタ ページの SiteName パブリック プロパティにアクセスする方法を次のコード例に示します。
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Title="MasterPage Example" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { MasterExample m = (MasterExample)Page.Master; mylabel.Text = m.SiteName; } </script> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> Hello, Master Pages! <asp:Label runat="server" Text="Label" ID="mylabel"></asp:Label> </asp:Content>
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="MasterPage Example" %> <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim m As MasterExample = CType(Page.Master, MasterPage) mylabel.Text = m.SiteName End Sub </script> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> Hello, Master Pages! <asp:Label runat="server" Text="Label" ID="mylabel"></asp:Label> </asp:Content>


System.Web.UI.Control
System.Web.UI.TemplateControl
System.Web.UI.UserControl
System.Web.UI.MasterPage


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


MasterPage コンストラクタ
アセンブリ: System.Web (system.web.dll 内)


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


MasterPage プロパティ



MasterPage メソッド



MasterPage メンバ
Content コントロールおよびそれぞれのコントロールのみで構成されるページのテンプレートおよびマージ コンテナとして機能します。
MasterPage データ型で公開されるメンバを以下の表に示します。






名前 | 説明 | |
---|---|---|
![]() | AbortTransaction | ユーザーがトランザクションを終了すると発生します。(TemplateControl から継承されます。) |
![]() | CommitTransaction | トランザクションが完了すると発生します。(TemplateControl から継承されます。) |
![]() | DataBinding | サーバー コントロールがデータ ソースに連結すると発生します。(Control から継承されます。) |
![]() | Disposed | サーバー コントロールがメモリから解放されると発生します。これは、ASP.NET ページが要求されている場合のサーバー コントロールの有効期間における最終段階です。(Control から継承されます。) |
![]() | Error | 未処理の例外がスローされると発生します。(TemplateControl から継承されます。) |
![]() | Init | サーバー コントロールが初期化されると発生します。これは、サーバー コントロールの有効期間における最初の手順です。(Control から継承されます。) |
![]() | Load | サーバー コントロールが Page オブジェクトに読み込まれると発生します。(Control から継承されます。) |
![]() | PreRender | Control オブジェクトの読み込み後、表示を開始する前に発生します。(Control から継承されます。) |
![]() | Unload | サーバー コントロールがメモリからアンロードされると発生します。(Control から継承されます。) |

「master page」の例文・使い方・用例・文例
- Master pageのページへのリンク