有一种方法可以做到这一点,但如果您依赖占位符中的任何默认内容,则在某些情况下会出现轻微问题。
在你的榜样,你有Parent1.master
:
<div id="content">
<h1>Lorem Ipsum, from Parent1</h1>
<asp:ContentPlaceHolder ID="cphContent" runat="server">
<p>I am default content from Parent1...</p>
</asp:ContentPlaceHolder>
</div>
,你也有一个嵌套Parent2.master
,消耗从Parent1占位符:
<asp:Content ContentPlaceHolderID="cphContent" runat="server">
<h2>I am some specific stuff from Parent2...</h2>
<asp:ContentPlaceHolder ID="cphContent" runat="server">
<p>I am default content from within Parent2!</p>
<p>We want to create another, nested CPH so that Parent3 can use it!</p>
<p>(It is seemingly OK that we can use the same ID for this CPH<br />
in Parent2 that we did originally in Parent1.)</p>
</asp:ContentPlaceHolder>
</asp:Content>
所以现在Parent3.master
可以使用来自占位符Parent2。 (并且还提供了另一种占位符,用于最终内容页消耗!)这就是:
<asp:Content ContentPlaceHolderID="cphContent" runat="server">
<h3>Hello from Parent3!</h3>
<asp:ContentPlaceHolder ID="cphContent" runat="server">
<p>I am more default text in yet another nested placeholder</p>
</asp:ContentPlaceHolder>
</asp:Content>
您呈现的内容页面会是这个样子:
<div id="content">
<h1>Lorem Ipsum, from Parent1</h1>
<h2>I am some specific stuff from Parent2...</h2>
<h3>Hello from Parent3!</h3>
<p>I am the plugged-in content, from the content page!</p>
</div>
一个很酷的事情对这种做法,并为什么我们可能希望在整个继承链中为这些嵌套的CPH使用相同的名称,最终的内容页面可能会从使用父主页面1到3的任何一个而不必更改其他任何内容,只要他们期望找到叫做cphContent
的东西来消费。好吧,现在你已经看到了有趣的部分,但是我提到的唯一可能是一个问题,就是如果你想让任何“默认”文本滴入任何大孩子。这意味着如果你的内容页面没有提供任何“cphContent”占位符的内容,那么只会使用最后一个母版页的默认值。从Parent1.master
的默认值实际上超出了Parent2。 (虽然你当然可以使用Parent3中的默认值)。可能有一种方法可以通过编程来实现,但是“开箱即用”,这似乎允许你做你所问的问题,如果你能忍受这个警告。
祝你好运!
不要认为你可以导航的父母的水平有任何限制。你可以发布你的代码 – 2010-09-29 18:18:27
有没有找到解决这个问题?我有同样的问题。 – Terry 2011-08-31 21:53:08