views:

105

answers:

2

I have some content from a CMS that I need to move to raw asp.net pages. Since the templates are nested I guess I can use nested masterpages to acomplish it, but I'm finding that I can't set values on the top masterpage from the deep child page.

Here is a sample. I have several nested masterpages with contentplaceholders:

  • top master (with contentPlaceHolder1)
  • nested master, dependent on top master (with contentPlaceHolder2)
  • aspx page, dependent on nested master, defines content for contentPlaceHolder1 and 2

The problem is that asp.net doesn't allow me to have the value of contentPlaceHolder1 defined in the content page, it should be defined in the nested master. But the point is that the client page knows that value, not the template masters (for instance, the page knows about the graphic it has to display on the the top, but the placeholder for the graphic is the top master).

How can I set values in the aspx page to be rendered in the top master?

A: 

I normally make all of my .aspx pages inherit from a base page, and on this page I set up properties for any data that I want to share. Then, on your top master page you can cast the current page to type basepage and then have access to all the data.

I usually expose the base page as a property on my master page also, that way I can do a soft cast and do null checking if I have any case where a page that doesn't inherit from my base page is using the master page.

jaltiere
+1  A: 

Normally you would have to do the following:

  1. setup a public property on your master page
  2. add the @MasterPage declaration to the top of any content page that you want to access the property in
  3. access the property like Master.YourPageProperty = "value";

In the case of nested masterpages you must also setup pass-through public properties using the same method above but doing it in your nested master page (ie define the nested master pages master page and set up dummy public properties that just set the value passed in to the nested master to the top master).

Try looking at this article to reinforce the ideas I have touched upon:

rtpHarry
I like the idea of the pass-through properties. It turns out you can also define kind of pass-through contentPlaceHolders in the nested master with something like this: <asp:Content ContentPlaceHolderID="opDetailImage" Visible="false" runat="server"><asp:ContentPlaceHolder ID="opDetailImage" runat="server"/></asp:Content>
David Suarez