Access to the content keeper of grandparents in the main pages

I searched the Internet and cannot find answers (there were several close questions about the stack overflow, but they did not seem to receive an answer or were not identical), so I thought that I would represent one of my own, It revolves around the embedded master pages and a content page that accesses the grandparent’s Content PlaceHolder, even if it doesn’t re-appear in the parent nested host. I wonder if this is not possible.

The main site. Master

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title> <asp:ContentPlaceHolder ID="TitleContent" runat="server"> <%= Html.GlobalModel().PageTitle %> </asp:ContentPlaceHolder> </title> <asp:ContentPlaceHolder ID="HeadContent" runat="server"> <link rel="shortcut icon" href="<%= ViewContext.ClientContent( "Content/Tiki.ico" ) %>" type="image/x-icon"/> </asp:ContentPlaceHolder> </head> <body> <asp:ContentPlaceHolder ID="SiteContent" runat="server"/> </body> </html> 

Nested Site.Master (note that TitleContent and HeadContent were not configured, so the default content from Core Site.Master should be affected)

 <%@ Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewMasterPage" %> <asp:Content ContentPlaceHolderID="SiteContent" runat="server"> <asp:ContentPlaceHolder ID="SiteContent" runat="server"> <h1>Nested Header</h1> <asp:ContentPlaceHolder ID="NestedContent" runat="server"/> </asp:ContentPlaceHolder> </asp:ContentPlaceHolder> 

ContentView.aspx (link to Nested Site.Master, attempting to replace TitleContent will not work.)

 <%@ Page Language="C#" MasterPageFile="Site.Master" %> <asp:Content ContentPlaceHolderID="NestedContent" runat="server"> <p>Nested content. This will work.</p> </asp:Content> <asp:Content ContentPlaceHolderID="TitleContent" runat="server"> Nested Title. This will **not** work. </asp:Content> 
+7
source share
1 answer

ContentPlaceHolderID can only refer to its immediate parent, if it is specified declaratively.

The simplest fix, though not the most elegant, is to copy the ContentPlaceHolders to the Nested Site.Master with the same default code. Some code duplication is required, but the job is in progress.

 <%@ Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewMasterPage" %> <asp:Content ContentPlaceHolderID="TitleContent" runat="server"> <asp:ContentPlaceHolder ID="NestedTitleContent" runat="server"> <%= Html.GlobalModel().PageTitle %> </asp:ContentPlaceHolder> </asp:ContentPlaceHolder> <asp:Content ContentPlaceHolderID="SiteContent" runat="server"> <asp:ContentPlaceHolder ID="SiteContent" runat="server"> <h1>Nested Header</h1> <asp:ContentPlaceHolder ID="NestedContent" runat="server"/> </asp:ContentPlaceHolder> </asp:ContentPlaceHolder> 

If you don’t want to do this, you can replace placeholders with custom controls that know what to show when.

Or, if you need to save it that way, you can run a bunch of code to force early rendering to a line / buffer in memory and replace child controls, but that will be a ton of problems, and it is doubtful if it will be worth the effort.

But any of these decisions depend on your situation. If you provided more context, we could provide more specific recommendations.

+6
source

All Articles