母板页的嵌套使用
出自:http://www.cnblogs.com/eddie005/services/trackbacks/442882.aspx
①首先是“父”母版页,与一般的母版页差不多,除了一些固定的内容,页面中间留下大片的“空白”区域:
 <%@ Master Language="C#" CodeFile="NestedMasterParent.master.cs" Inherits="NestedMasterParent_master" %>
<%@ Master Language="C#" CodeFile="NestedMasterParent.master.cs" Inherits="NestedMasterParent_master" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<head runat="server"> <title>MasterPage可以多层嵌套使用</title>
    <title>MasterPage可以多层嵌套使用</title> </head>
</head> <body>
<body> <form id="form1" runat="server">
    <form id="form1" runat="server"> <table border="1" cellpadding="0" cellspacing="0" width="100%" height="100%">
    <table border="1" cellpadding="0" cellspacing="0" width="100%" height="100%"> <tr>
        <tr> <td colspan="2" align="center" style="background-color: mediumturquoise" >MasterPage可以多层嵌套使用~~~</td>
            <td colspan="2" align="center" style="background-color: mediumturquoise" >MasterPage可以多层嵌套使用~~~</td> </tr>
        </tr> <tr>
        <tr> <td valign="top" width="150" class="nav" style="background-color: lightcyan">
            <td valign="top" width="150" class="nav" style="background-color: lightcyan"> <table border="0" width="100%" cellpadding="4" cellspacing="0">
                <table border="0" width="100%" cellpadding="4" cellspacing="0"> <tr>
                    <tr> <td><a href="#">超链接一</a></td>
                        <td><a href="#">超链接一</a></td> </tr>
                    </tr> <tr>
                    <tr> <td><a href="#">超链接二</a></td>
                        <td><a href="#">超链接二</a></td> </tr>
                    </tr> <tr>
                    <tr> <td><a href="#">超链接三</a></td>
                        <td><a href="#">超链接三</a></td> </tr>
                    </tr> <tr>
                    <tr> <td><a href="#">超链接四</a></td>
                        <td><a href="#">超链接四</a></td> </tr>
                    </tr> <tr>
                    <tr> <td><a href="#">…………</a></td>
                        <td><a href="#">…………</a></td> </tr>
                    </tr> <tr>
                    <tr> <td style="font-size: x-small; color: forestgreen; ">(上面这些链接都是假的,点了也没什么效果)</td>
                        <td style="font-size: x-small; color: forestgreen; ">(上面这些链接都是假的,点了也没什么效果)</td> </tr>
                    </tr> </table>
                </table> </td>
            </td> <td valign="top" align="center">
            <td valign="top" align="center"> <asp:contentplaceholder id="ChildMaster" runat="server" />
                <asp:contentplaceholder id="ChildMaster" runat="server" /> </td>
            </td> </tr>
        </tr> <tr>
        <tr> <td colspan="2" align="center" style="background-color: lightyellow" ><a href="#" onclick="window.close();">GoodBye</a></td>
            <td colspan="2" align="center" style="background-color: lightyellow" ><a href="#" onclick="window.close();">GoodBye</a></td> </tr>
        </tr> </table>
    </table> </form>
    </form> </body>
</body> </html>
</html>
②然后是“子”母版页,它填充了上面的母板页所留下的空白,同时,再次留下3块“空白”的区域:
 <%@ Master Language="C#" masterpagefile="~/NestedMasterParent.master" CodeFile="NestedMasterChild.master.cs" Inherits="NestedMasterChild_master" %>
<%@ Master Language="C#" masterpagefile="~/NestedMasterParent.master" CodeFile="NestedMasterChild.master.cs" Inherits="NestedMasterChild_master" %>
 <asp:content id="ChildMaster" contentplaceholderid="ChildMaster" runat="Server">
<asp:content id="ChildMaster" contentplaceholderid="ChildMaster" runat="Server"> <table border="1" cellpadding="10" cellspacing="0" width="100%" height="100%">
<table border="1" cellpadding="10" cellspacing="0" width="100%" height="100%"> <tr>
    <tr> <td align="left" colspan="2" style="font-style: italic;">“子”母版页:
        <td align="left" colspan="2" style="font-style: italic;">“子”母版页: </td>
        </td> </tr>
    </tr> <tr>
    <tr> <td align="left" colspan="2" >
        <td align="left" colspan="2" > <asp:contentplaceholder id="ContentTitle" runat="Server" />
            <asp:contentplaceholder id="ContentTitle" runat="Server" /> </td>
        </td> </tr>
    </tr> <tr>
    <tr> <td align="left" valign="top" >
        <td align="left" valign="top" > <asp:contentplaceholder id="ContentText" runat="Server" />
            <asp:contentplaceholder id="ContentText" runat="Server" /> </td>
        </td> <td align="left" width="150" valign="top" >
        <td align="left" width="150" valign="top" > <asp:contentplaceholder id="ContentNav" runat="Server" />
            <asp:contentplaceholder id="ContentNav" runat="Server" /> </td>
        </td> </tr>
    </tr> </table>
</table> </asp:content>
</asp:content>
稍稍遗憾的是,上面的HTML只能手写,因为不但我们在添加一个新母版页时无法勾选“选择母版页”,并且还无法切换到设计视图:
③最后是使用母版页的Page:
 <%@ Page Language="C#" masterpagefile="~/NestedMasterChild.master" CodeFile="NestedMaster.aspx.cs" Inherits="NestedMaster_aspx" Title="Nested Master Pages" %>
<%@ Page Language="C#" masterpagefile="~/NestedMasterChild.master" CodeFile="NestedMaster.aspx.cs" Inherits="NestedMaster_aspx" Title="Nested Master Pages" %>
 <asp:content id="ContentTitle" contentplaceholderid="ContentTitle" runat="Server">
<asp:content id="ContentTitle" contentplaceholderid="ContentTitle" runat="Server"> <h1>区域A:Content Title</h1>
<h1>区域A:Content Title</h1> </asp:content>
</asp:content>
 <asp:content id="ContentText" contentplaceholderid="ContentText" runat="Server">
<asp:content id="ContentText" contentplaceholderid="ContentText" runat="Server"> <p>区域B</p>
<p>区域B</p> </asp:content>
</asp:content>
 <asp:content id="ContentNav" contentplaceholderid="ContentNav" runat="Server">
<asp:content id="ContentNav" contentplaceholderid="ContentNav" runat="Server"> <p>区域C</p>
<p>区域C</p> </asp:content>
</asp:content>
同样遗憾的是,设计上面的页面时也无法切换到设计视图。
在页面中的各个区域里设计好适当的内容后,运行起来,可以得到下面的效果:
这时候我们也许会有一个疑问:为什么要用多层嵌套的母版页?象上面的例子,一开始就在页面上
留下3块区域不是更方便吗?
大概应该这样考虑:站点可能使用一个统一的母版页所为主框架,然后在一片较大的区域中,某一些情况需要分成3块的样式,某一些则会是4块,也许还会有更复杂的要求,这时候多层嵌套就有用了。
但是问题又来了:设计起来这么不方便,该如何使用呢?
 
                    
                     
                    
                 
                    
                 


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号