简体   繁体   English

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

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

I have a 3 level nested master pages and a content page. 我有一个3级嵌套母版页和一个内容页面。 parent1 is the top parent, parent2 is parent of parent3 and parent3 is the parent of the content page. parent1是顶级父级,parent2是parent3的父级,parent3是内容页面的父级。

I get an error ' Cannot find ContentPlaceHolder xxx... ' where xxx is a ContentPlaceholder. 我收到错误' Cannot find ContentPlaceHolder xxx... ',其中xxx是ContentPlaceholder。 It resides in parent2 and content page is trying to fill it. 它位于parent2中,内容页面正在尝试填充它。

Can content pages only use their direct parent ContentPlaceHolders or can they also use any of the higher master pages? 内容页面只能使用他们的直接父ContentPlaceHolders,还可以使用任何更高版本的母版页?

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 : 在您的示例中,您有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: 而且你还有一个嵌套的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>

And so now Parent3.master can consume the placeholder from Parent2. 所以现在Parent3.master可以使用Parent3.master中的占位符。 (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. 关于这种方法的一个很酷的事情,以及为什么我们可能希望在整个继承链中对这些嵌套的CPH使用相同的名称,是因为您的最终内容页面可能会从使用任何父母版页1到3更改而无需更改任何内容否则,只要他们期望找到一个叫做cphContent东西来消费。

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. 这样,我的意思是如果您的内容页面没有为“cphContent”占位符提供任何内容,那么将只使用上一个母版页的默认值。 The default from Parent1.master is essentially lost beyond Parent2. Parent1.master的默认值基本上不会超过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. (虽然您当然可以使用Parent3中的默认值。)可能有一种方法可以以编程方式执行此操作,但“开箱即用”这似乎可以让您按照您的要求执行操作,如果您可以忍受此警告。

Best of luck! 祝你好运!

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

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. (如果母版页包含ContentPlaceHolder控件中的控件,则如果内容页面中的内容控件覆盖了这些控件,则无法访问这些控件。)控件不能作为母版页成员直接访问,因为它们受到保护。 However, you can use the FindControl method to locate specific controls on the master page. 但是,您可以使用FindControl方法在母版页上查找特定控件。 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. 如果要访问的控件位于母版页上的ContentPlaceHolder控件内,则必须先获取对ContentPlaceHolder控件的引用,然后调用其FindControl方法以获取对该控件的引用。

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. 引用的控件之一是在ContentPlaceHolder控件中,而另一个不是。

Visual Basic Copy Code ' Gets a reference to a TextBox control inside a 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

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 由于您要查找嵌套内容占位符,因此您可能必须找到父级,然后使用该实例查找子级

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

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