内容页面可以使用它的母版页的主父ContentPlaceHolderID(嵌套母版页) [英] Can content page use ContentPlaceHolderID of master parent of its master page (nested master pages)

查看:122
本文介绍了内容页面可以使用它的母版页的主父ContentPlaceHolderID(嵌套母版页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3级嵌套母版页和内容页。 parent1是顶级父,parent2是parent3的父母和parent3是内容页的父。

I have a 3 level nested master pages and a content page. parent1 is the top parent, parent2 is parent of parent3 and parent3 is the parent of the content page.

我得到一个错误找不到的ContentPlaceHolder XXX ... ,其中xxx是的Co​​ntentPlaceHolder。它位于parent2和内容页试图填补它。

I get an error 'Cannot find ContentPlaceHolder xxx...' where xxx is a ContentPlaceholder. It resides in parent2 and content page is trying to fill it.

可以将内容页只能使用其直接父ContentPlaceHolders或者他们也使用任何更高的母版页?

Can content pages only use their direct parent ContentPlaceHolders or can they also use any of the higher master pages?

推荐答案

还有就是要做到这一点的一种方式,但有一个与它在某些情况下,如果你是靠从一个占位符的默认内容一个小问题。

There is one way to do this but there is a slight problem with it under certain circumstances if you are relying on any default content from a placeholder.

在你的榜样,你有 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消耗占位符:

And you also have a nested Parent2.master, which consumes the placeholder from 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的占位符。 (并且还提供了另一种占位符,最终内容页消耗!)这是:

And so now Parent3.master can consume the placeholder from Parent2. (And also provide another placeholder for eventual content page to consume!) Here it is:

<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>

您呈现的内容网页看起来是这样的:

Your rendered content page would look something like this:

<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>

对这种做法,为什么我们可能希望使用在整个产业链中嵌套CPHS相同的名称一个很酷的事情,那是你的最终内容页面可以使用任何家长的母版页1到3,而无需改变因为他们希望找到 cphContent 所谓消费改变任何东西,这么久。

One cool thing about this approach, and why we might want to use the same names for these nested CPHs throughout the inheritance chain, is that your eventual content page could change from using any of the Parent master pages 1 through 3 without having to change anything else, so long as they expected to find something called cphContent to consume.

好了,现在你已经看到了有趣的部分,但是我提到的可能是一个问题的唯一的事情是,如果你试图让任何默认的文字滴入任何盛大的孩子。这样,我的意思是,如果你的内容页面不为cphContent占位符提供任何内容,那么只能从最后一个母版页默认将被使用。从 Parent1.master 默认基本上失去了超越Parent2。 (虽然你当然可以使用来自Parent3默认设置。)有可​​能是一种以编程方式做到这一点,但开箱即用,这似乎让你做你问什么,如果你可以用这个警告生活。

OK, so now you have seen the fun part, but the only thing I mentioned might be a problem, is if you were trying to let any of the "default" text trickle down to any of the grand-children. By this, I mean that if your content page doesn't supply any Content for the "cphContent" placeholder, then only the default from the last master page would be used. The default from Parent1.master is essentially lost beyond Parent2. (Although you could certainly use the default from Parent3.) There may be a way to do this programmatically, but "out of the box" this seems to allow you to do what you asked, if you can live with this caveat.

祝你好运!

这篇关于内容页面可以使用它的母版页的主父ContentPlaceHolderID(嵌套母版页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆