まずはマスターページ「Site1.Master」を追加。

【マスターページ「Site1.Master」】

1<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Sample.Site1" %>
2 
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5<head runat="server">
6    <title></title>
7    <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>
8</head>
9<body>
10    <form id="form1" runat="server">
11    <div>
12        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
13    </div>
14    </form>
15</body>
16</html>

次に入れ子にされたマスターページ「NestedMasterPage1.master」を追加し、マスターページに「Site1.Master」を指定。

作成された「NestedMasterPage1.master」にContentPlaceHolderを2つ追加。

これを書いて置かないと「NestedMasterPage1.master」をマスターページとするフォーム等にasp:Contentタグが生成されない。
(※特に調べもせずになんとなーくで入れ子をしていた時、とりあえずここでつまづいた。)

【入れ子にされたマスターページ「NestedMasterPage1.master」】

1<%@ Master Language="C#" MasterPageFile="~/Sample/Site1.Master" AutoEventWireup="true"
2    CodeBehind="NestedMasterPage1.master.cs" Inherits="WebApplication1.Sample.NestedMasterPage1" %>
3 
4<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
5    <!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="head")に反映される。-->
6    <!--ネストさせるために下のContentPlaceHolderを追加-->
7    <asp:ContentPlaceHolder ID="Nestedhead" runat="server"></asp:ContentPlaceHolder>
8</asp:Content>
9<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
10    <!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="ContentPlaceHolder1")に反映される。-->
11    <!--ネストさせるために下のContentPlaceHolderを追加-->
12    <asp:ContentPlaceHolder ID="NestedContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>
13</asp:Content>

次にマスターページを使用したWebフォーム「WebForm1.aspx」を追加し、マスターページに「NestedMasterPage1.master」を指定。

【マスターページを使用したWebフォーム「WebForm1.aspx」】

1<asp:Content ID="Content1" ContentPlaceHolderID="Nestedhead" runat="server">
2    <!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="Nestedhead")に反映される。-->
3</asp:Content>
4<asp:Content ID="Content2" ContentPlaceHolderID="NestedContentPlaceHolder1" runat="server">
5    <!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="NestedContentPlaceHolder1")に反映される。-->
6</asp:Content>

「NestedMasterPage1.master」でContentPlaceHolderを追加しておいたので、そのContentPlaceHolderにひもづくasp:Contentタグが生成されている。

上記、3つのファイルを作成し、WebForm1.aspxをスタートページに設定して実行。

ソースを確認してみる。

【WebForm1.aspxをスタートページにして、実行した結果のソース】

1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<head><title>
4 
5</title>
6    <!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="head")に反映される。-->
7    <!--ネストさせるために下のContentPlaceHolderを追加-->
8     
9    <!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="Nestedhead")に反映される。-->
10 
11</head>
12<body>
13    <form method="post" action="WebForm1.aspx" id="form1">
14<div class="aspNetHidden">
15<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTY1NDU2MTA1MmRkP87F4qPZjR5YK6bxld9tyMWDkLHWVT2qCGwoOKNobac=" />
16</div>
17 
18    <div>
19         
20    <!--ここに書いた内容はSite1.MasterのContentPlaceHolder(ID="ContentPlaceHolder1")に反映される。-->
21    <!--ネストさせるために下のContentPlaceHolderを追加-->
22     
23    <!--ここに書いた内容はNestedMasterPage1.MasterのContentPlaceHolder(ID="NestedContentPlaceHolder1")に反映される。-->
24 
25 
26    </div>
27    </form>
28</body>
29</html>

各ファイルに記述したソースはこのように表示される。

今回の仕事で作るシステムの画面は上部に全画面共通のヘッダーメニュー、左側にサブ機能毎に異なるサイドメニュー、右側に各画面表示というレイアウトだったので、マスターページを使ってみた。

前はフレームで切ったり、ユーザーコントロールを作ったりしてたけど、マスターページが便利そう。

「.master」から「.aspx」のコントロールを参照することもできるし、もちろん逆に「.aspx」から「.master」のコントロールを参照することもできるから、ガンガン使いこなせるようにいろいろ試してみよう。

ちなみに参照するときの方法として、FindControlで探す方法があるんだけど、FindControlっていろいろコントロール見つけてきちゃうからめちゃめちゃすごいと思う。

まぁそうゆうメソッドなんだけどさ。

結構お世話になってるからなぁ。

ただ、FindControlは遅くなるって聞いたことあるけどね。。。(本当かどうかは知らない)

コメントを残す

メールアドレスが公開されることはありません。