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>
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>
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.
I am using asp.net 3.0 in Visual Studio 2008
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.
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.
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.
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?
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With