内容页是否可以使用其母版页的母版父级的 ContentPlaceHolderID(嵌套母版页)

发布于 2024-09-25 14:33:35 字数 253 浏览 2 评论 0原文

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

我收到错误“找不到 ContentPlaceHolder xxx...”,其中 xxx 是 ContentPlaceholder。它驻留在parent2 中,内容页面正在尝试填充它。

内容页面是否只能使用其直接父级 ContentPlaceHolders 还是也可以使用任何更高的母版页?

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笔芯 2024-10-02 14:33:35

有一种方法可以做到这一点,但在某些情况下,如果您依赖占位符中的任何默认内容,那么它会出现一些小问题。

在您的示例中,您有 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 的默认值。)可能有一种方法可以以编程方式执行此操作,但“开箱即用”这似乎允许您执行您所要求的操作,如果您可以接受此警告。

祝你好运!

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.

In your example, you have 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>

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>

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>

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.

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.

Best of luck!

偏爱自由 2024-10-02 14:33:35

我相信内容页面只能使用直接父级的ContentPlaceHolder。

I believe content pages can only use the ContentPlaceHolder of the direct parent.

东走西顾 2024-10-02 14:33:35

获取母版页上控件的值
在运行时,母版页与内容页合并,因此内容页代码可以访问母版页上的控件。 (如果母版页包含 ContentPlaceHolder 控件中的控件,则这些控件如果被内容页中的内容控件覆盖,则无法访问。)这些控件不能作为母版页成员直接访问,因为它们受到保护。但是,您可以使用 FindControl 方法来定位母版页上的特定控件。如果要访问的控件位于母版页上的 ContentPlaceHolder 控件内,则必须首先获取对 ContentPlaceHolder 控件的引用,然后调用其 FindControl 方法以获取对该控件的引用。

以下示例显示如何获取对母版页上的控件的引用。所引用的控件之一位于 ContentPlaceHolder 控件中,而另一个则不在。

Visual Basic 复制代码
' 获取对 ContentPlaceHolder 内 TextBox 控件的引用

Dim mpContentPlaceHolder As ContentPlaceHolder
Dim mpTextBox As TextBox
mpContentPlaceHolder = _
    CType(Master.FindControl("ContentPlaceHolder1"), _
    ContentPlaceHolder)
If Not mpContentPlaceHolder Is Nothing Then
    mpTextBox = CType(mpContentPlaceHolder.FindControl("TextBox1"), _
        TextBox)
    If Not mpTextBox Is Nothing Then
        mpTextBox.Text = "TextBox found!"
    End If

由于您想查找嵌套的 Content 占位符,您可能必须找到父级,然后使用该实例来查找子级

Getting the Values of Controls on the Master Page
At run time, the master page is merged with the content page, so the controls on the master page are accessible to content page code. (If the master page contains controls in a ContentPlaceHolder control, those controls are not accessible if overridden by a Content control from the content page.) The controls are not directly accessible as master-page members because they are protected. However, you can use the FindControl method to locate specific controls on the master page. If the control that you want to access is inside a ContentPlaceHolder control on the master page, you must first get a reference to the ContentPlaceHolder control, and then call its FindControl method to get a reference to the control.

The following example shows how you can get a reference to controls on the master page. One of the controls being referenced is in a ContentPlaceHolder control and the other is not.

Visual Basic Copy Code
' Gets a reference to a TextBox control inside a ContentPlaceHolder

Dim mpContentPlaceHolder As ContentPlaceHolder
Dim mpTextBox As TextBox
mpContentPlaceHolder = _
    CType(Master.FindControl("ContentPlaceHolder1"), _
    ContentPlaceHolder)
If Not mpContentPlaceHolder Is Nothing Then
    mpTextBox = CType(mpContentPlaceHolder.FindControl("TextBox1"), _
        TextBox)
    If Not mpTextBox Is Nothing Then
        mpTextBox.Text = "TextBox found!"
    End If

Since you want to Find a nested Content place holder you may have to find the parent then use that instance to find the child

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文