有一种方法可以做到这一点,但在某些情况下,如果您依赖占位符中的任何默认内容,那么它会出现一些小问题。
在你的例子中,你有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 的默认值。)可能有一种方法可以以编程方式执行此操作,但“开箱即用”这似乎允许您执行您所要求的操作,如果您可以接受此警告。
祝你好运!