Can a content page use the ContentPlaceHolderID of the main parent of the main page (nested master pages)

I have 3-level nested master pages and a content page. parent1 is the top parent, parent2 is the 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 is in parent2, and the content page is trying to populate it.

Can content pages use only their parent parent ContentPlaceHolders, or can they use any of the top master pages?

+3
source share
3

, , .

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, placeholder 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 placeholder 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.) , , " ", , , , .

!

+1

, ContentPlaceHolder .

0

, . ( ContentPlaceHolder, , Content .) -, . , FindControl . , , ContentPlaceHolder , ContentPlaceHolder, FindControl, .

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

Visual Basic Script Code 'Gets a link 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 storage space holder, you may need to find the parent and then use this instance to find the child

0
source

Source: https://habr.com/ru/post/1767221/


All Articles