Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ContentPlaceHolders: Repeated Content

Scenario

I have an application using asp.net Master Pages in which I would like to repeat some content at the top and bottom of a page. Currently i use something like this:

Master Page
<html>
  <body>
    <asp:ContentPlaceHolder ID="Foo" runat="server">
    </asp:ContentPlaceHolder>
    <!-- page content -->
    <asp:ContentPlaceHolder ID="Bar" runat="server">
    </asp:ContentPlaceHolder>
  </body>
</html>
Content Page
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
  <!-- content -->
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
  <!-- content repeated -->
</asp:Content>

Maintenance

As you know, repeating things in code is usually not good. It creates maintenance problems. The following is what I would like to do but will obviously not work because of the repeated id attribute:

Master Page
<html>
  <body>
    <asp:ContentPlaceHolder ID="Foo" runat="server">
    </asp:ContentPlaceHolder>
    <!-- page content -->
    <asp:ContentPlaceHolder ID="Foo" runat="server">
    </asp:ContentPlaceHolder>
  </body>
</html>
Content Page
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
  <!-- content (no repetition) -->
</asp:Content>

Possible?

Is there a way to do this using asp.net webforms? The solution does not necessarily have to resemble the above content, it just needs to work the same way.

Notes

I am using asp.net 3.0 in Visual Studio 2008

like image 305
brad Avatar asked Apr 17 '09 13:04

brad


People also ask

What is a ContentPlaceHolder?

Please Sign up or sign in to vote. A ContentPlaceHolder control defines a relative region for content in a master page, and renders all text, markup, and server controls from a related Content control found in a content page. A Content control is associated with a ContentPlaceHolder using its ContentPlaceHolderID property.

Is there a content control for the leftcolumncontent?

Currently, Default.aspx contains two Content controls for the head and MainContent ContentPlaceHolders; it does not have a Content control for LeftColumnContent. Consequently, when Default.aspx is rendered the LeftColumnContent ContentPlaceHolder's default content is used.

How to avoid duplicate content issues when syndicating?

If you intentionally syndicate content to other websites, then it’s worth asking them to add a canonical link to the original. That will eliminate the risk of duplicate content issues.

Can republished content outrank the original on Google?

It’s only when the scraped or republished content starts outranking the original on your site that issues arise. The good news is this is a rare occurrence, but it can happen. Does Google have a duplicate content penalty?


2 Answers

Hey, if someone still needs a solution for this here is what I did: Put it in the Code File of your master page

protected override void RenderChildren(HtmlTextWriter writer)
{
    Control mainPlaceHolder = FindControl("MainContentPlaceHolder");
    Control doublePlaceHolder = FindControl("ContentDoublePlaceHolder");

    StringBuilder sb = new StringBuilder();

    using (StringWriter sw = new StringWriter(sb))
    using (HtmlTextWriter tw = new HtmlTextWriter(sw))
    {
        mainPlaceHolder.RenderControl(tw);
    }

    LiteralControl lc = new LiteralControl(sb.ToString());
    doublePlaceHolder.Controls.Add(lc);

    base.RenderChildren(writer);
}

I'm not sure about performance issues here, but it certainly works.

like image 103
Egor Pavlikhin Avatar answered Sep 28 '22 08:09

Egor Pavlikhin


In the case where you just want to repeat a string, this worked for me:

<asp:ContentPlaceHolder ID="TitleContent" runat="server" /> 

Can be referenced elsewhere as:

<%  Dim title As LiteralControl = TitleContent.Controls.Item(0)
    Response.Write(title.Text)
%>

No warranty on that being the right course of action though :)

like image 23
Dave Avatar answered Sep 28 '22 06:09

Dave