Search code examples
asp.netmaster-pagescontentplaceholder

asp.net: does master pages inheritance doesn't inherit placeholders?


Imagine you have two masterpages:

MasterPage "A": PlaceHolder-A1 PlaceHolder-A2

MaterPage "B": (inherits from A) Populates "PlaceHolder-A1"

I have to create ordinary aspx HTML page that inherits from "B" & populates "PlaceHolder-A2" (from MasterPage "A") ?

How can I do that?

The only way I found is to translate "PlaceHolder-A2" down to MasterPage "B" by placeholder "PlaceHolder-B1" that is empty and inside "PlaceHolder-A2". Page should populate "PlaceHolder-B1" then.

Is there a simpler way to do that ?

Thank you in advance!


Solution

  • Since your question is, is there a "simpler" way to do this, I'd say the answer is no. Really all you need to do is make sure the contentplaceholders propagate down through each masterpage. You can even use the same name. I use nested masterpages all the time and, for instance, I always propagate a placeholder for the <head> area of the page:

    <asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="Server">
        <asp:ContentPlaceHolder ID="Head" runat="server">
        </asp:ContentPlaceHolder>
    </asp:Content>
    

    I don't see what's so complicated about that? Do you not have access to change one of the master pages for some reason? If so then you would be forced to access the ContentPlaceHolder programatically similar to what Precious Roy is depicting (Although I'm not sure if his example is 100% right for the scenario you described).