有一種方法可以做到這一點,但如果您依賴佔位符中的任何默認內容,則在某些情況下會出現輕微問題。
在你的榜樣,你有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