まずはマスターページ「Site1.Master」を追加。
【マスターページ「Site1.Master」】
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Sample.Site1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
次に入れ子にされたマスターページ「NestedMasterPage1.master」を追加し、マスターページに「Site1.Master」を指定。
作成された「NestedMasterPage1.master」にContentPlaceHolderを2つ追加。
これを書いて置かないと「NestedMasterPage1.master」をマスターページとするフォーム等にasp:Contentタグが生成されない。
(※特に調べもせずになんとなーくで入れ子をしていた時、とりあえずここでつまづいた。)
【入れ子にされたマスターページ「NestedMasterPage1.master」】
<%@ Master Language="C#" MasterPageFile="~/Sample/Site1.Master" AutoEventWireup="true"
CodeBehind="NestedMasterPage1.master.cs" Inherits="WebApplication1.Sample.NestedMasterPage1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="head")に反映される。-->
<!--ネストさせるために下のContentPlaceHolderを追加-->
<asp:ContentPlaceHolder ID="Nestedhead" runat="server"></asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="ContentPlaceHolder1")に反映される。-->
<!--ネストさせるために下のContentPlaceHolderを追加-->
<asp:ContentPlaceHolder ID="NestedContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
</asp:Content>
次にマスターページを使用したWebフォーム「WebForm1.aspx」を追加し、マスターページに「NestedMasterPage1.master」を指定。
【マスターページを使用したWebフォーム「WebForm1.aspx」】
<asp:Content ID="Content1" ContentPlaceHolderID="Nestedhead" runat="server">
<!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="Nestedhead")に反映される。-->
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="NestedContentPlaceHolder1" runat="server">
<!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="NestedContentPlaceHolder1")に反映される。-->
</asp:Content>
「NestedMasterPage1.master」でContentPlaceHolderを追加しておいたので、そのContentPlaceHolderにひもづくasp:Contentタグが生成されている。
上記、3つのファイルを作成し、WebForm1.aspxをスタートページに設定して実行。
ソースを確認してみる。
【WebForm1.aspxをスタートページにして、実行した結果のソース】
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="head")に反映される。-->
<!--ネストさせるために下のContentPlaceHolderを追加-->
<!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="Nestedhead")に反映される。-->
</head>
<body>
<form method="post" action="WebForm1.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1MmRkP87F4qPZjR5YK6bxld9tyMWDkLHWVT2qCGwoOKNobac=" />
</div>
<div>
<!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="ContentPlaceHolder1")に反映される。-->
<!--ネストさせるために下のContentPlaceHolderを追加-->
<!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="NestedContentPlaceHolder1")に反映される。-->
</div>
</form>
</body>
</html>
各ファイルに記述したソースはこのように表示される。
今回の仕事で作るシステムの画面は上部に全画面共通のヘッダーメニュー、左側にサブ機能毎に異なるサイドメニュー、右側に各画面表示というレイアウトだったので、マスターページを使ってみた。
前はフレームで切ったり、ユーザーコントロールを作ったりしてたけど、マスターページが便利そう。
「.master」から「.aspx」のコントロールを参照することもできるし、もちろん逆に「.aspx」から「.master」のコントロールを参照することもできるから、ガンガン使いこなせるようにいろいろ試してみよう。
ちなみに参照するときの方法として、FindControlで探す方法があるんだけど、FindControlっていろいろコントロール見つけてきちゃうからめちゃめちゃすごいと思う。
まぁそうゆうメソッドなんだけどさ。
結構お世話になってるからなぁ。
ただ、FindControlは遅くなるって聞いたことあるけどね。。。(本当かどうかは知らない)
