繁体   English   中英

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

[英]Can content page use ContentPlaceHolderID of master parent of its master page (nested master pages)

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

我收到错误' Cannot find ContentPlaceHolder xxx... ',其中xxx是ContentPlaceholder。 它位于parent2中,内容页面正在尝试填充它。

内容页面只能使用他们的直接父ContentPlaceHolders,还可以使用任何更高版本的母版页?

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

在您的示例中,您有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可以使用Parent3.master中的占位符。 (还提供另一个占位符,供最终内容页面使用!)这里是:

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

祝你好运!

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

获取主页面上的控件值在运行时,主页面与内容页面合并,因此主页面上的控件可供内容页面代码访问。 (如果母版页包含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

由于您要查找嵌套内容占位符,因此您可能必须找到父级,然后使用该实例查找子级

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM