开发者

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

Imagine you have two masterpages:

Maste开发者_StackOverflow中文版rPage "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!


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).


Easier is a relative term here. There is no easier way to handle it in regards to associating master pages and their placeholders, but it is possible to set it from the code behind. See this question for reference. Roadie57 explains it better than I can. Not sure if you're using VB, but if not you can do it like this:

ContentPlaceHolder mpContentPlaceHolder = new ContentPlaceHolder();
TextBox mpTextBox = new TextBox();
mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("PlaceHolderA2");
if (mpContentPlaceHolder != null)
{
    ...place content in mpContentPlaceHolder
}


That's sounds a little complicated.. as you as you have some common parts between pages you might consider of using 'User controls' for that. You might place user control whatever place on page you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜