How do I put a placeholder twice on master page?

1k Views Asked by At

I have a master page and I want to add a placeholder twice - so that I have the same placeholder in two places of the master page so that actual page just specifies content of the placeholder once and that content is rendered twice on the resulting page. The goal is to avoid duplication of content.

If I try to add a placeholder with the same id twice it won't compile - ASP.NET doesn't like that.

How do I achieve that? What are other options?

3

There are 3 best solutions below

0
On

I haven't tried this, but you could lookup the contents of the placeholder you are interested in duplicating, and copying it in your code behind. This post: http://programcsharp.com/blog/archive/2009/01/22/test-if-masterpage-contentplaceholder-has-content-or-is-empty.aspx

has some good insight into manipulating master pages.

0
On

I think you are probably looking for a user control:

http://msdn.microsoft.com/en-us/library/y6wb1a0e.aspx

With User Controls you can add your markup and code behind into an .ascx and then call this in your Master Page/Web Form multiple times, therefore avoiding duplicate code.

0
On

Place your placeholder in an Action, and call it where necessary.

<%
    Action myPlaceholder = () =>
    {%>
        <asp:ContentPlaceHolder ID="X" runat="server" />
    <%}
%>

...then call wherever necessary in code.

<% if (conditionMet)
    myPlaceholder(); >%

The error occurs at compile time: a complaint will be made if placeholders with the same ID exist. This approach clears that hurdle, and gives a lot of flexibility.