Dynamic Master Page Selection - Acessing ContentPlaceholders

Last post 02-11-2007 11:03 AM by Rauland. 6 replies.

Sort Posts:

  • Dynamic Master Page Selection - Acessing ContentPlaceholders

    08-04-2005, 4:13 PM
    • Member
      144 point Member
    • jatfill
    • Member since 07-23-2003, 9:29 AM
    • Charlotte, NC
    • Posts 40
    I have been working with master pages for a very short time, so I apologize if this is a simple question.

    I am creating a content page that will (in theory) use one of multiple master pages available through my web site. The PreInit method seems to easily handle this change:


    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

       Me.MasterPageFile = "~/Master_Pages/MinimalLayout.master"

    End Sub

    The issue I am running into right now is that I am unclear on how to get the page to show the default content from each master page when dynamically changing the master file. Each master file has a varying number/position/style of placeholders, so I would ideally like to be able to populate the content in a dynamic fashion. I tried adding the content control(s) in preinit and associating them to each master placeholder, but I get an error message that says this:

    Setting the ContentPlaceHolderID property of System.Web.UI.WebControls.Content is not supported.

    here's the code I tried; the intial call in PageInit is AddPlaceholderControls(Me.Master):

    Private Sub AddPlaceholderControls(ByVal oParent As Control)

       Dim oContent As Content

       For Each oCtl As Control In oParent.Controls
          
          If
    TypeOf oCtl Is ContentPlaceHolder Then
             oContent = New Content
             With oContent
                .ContentPlaceHolderID = oCtl.ID
             End With
             Me.Controls.Add(oContent)

          ElseIf oCtl.HasControls Then
             AddPlaceholderControls(oCtl)
          End If

       Next

    End Sub

    Any assistance or direction would be greatly appreciated!

  • Re: Dynamic Master Page Selection - Acessing ContentPlaceholders

    08-05-2005, 4:32 AM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815
    Why not just have the ID of the ContentPlaceHolder the same across both MasterPages?   then when your aspx page says: "go fill Content Place Holder 'Holder1' with this stuff", it doesn't matter what MasterPage it is
    "If you make it idiot proof, they'll build a better idiot"
  • Re: Dynamic Master Page Selection - Acessing ContentPlaceholders

    08-05-2005, 8:47 AM
    • Member
      144 point Member
    • jatfill
    • Member since 07-23-2003, 9:29 AM
    • Charlotte, NC
    • Posts 40
    Good point... and yes, that will work. I'm thinking more from the perspective of having varying/multiple placeholders, depending on the master page. The way it's set up right now I don't think that I would be able to do this without having an aspx page for each template. Seems like this defeats part of the intent of master pages...?
  • Re: Dynamic Master Page Selection - Acessing ContentPlaceholders

    08-05-2005, 10:23 AM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815

     jatfill wrote:
    Seems like this defeats part of the intent of master pages...?

    Just my 2 pennies:

    I don't agree with that thinking, as the MasterPage is a design structure that is independant of the content

    Like the setup i have is one single set of code pushing out content/functionality to two different websites/urls, same site, different look (which sounds like what you are after with a master page called "MinimalLayout" (maybe i'm wrong though)

    If the content does vary based on master page, i'd go about using User Controls for the content and dynamically load them based on whatever criteria is deciding your master page

    "If you make it idiot proof, they'll build a better idiot"
  • Sv.: Dynamic Master Page Selection - Acessing ContentPlaceholders

    11-05-2005, 5:46 PM
    • Member
      82 point Member
    • cultx
    • Member since 11-05-2005, 2:13 PM
    • Denmark
    • Posts 20
    Hi

    Having exactly the same problem, and I have to load the Content dynamicly, as I use many different MasterPages with different numbers of PlaceHolders...

    Have you found a solution on this problem or just using the "work around" descriped bellow...?
  • Devil [666] Re: Dynamic Master Page Selection - Acessing ContentPlaceholders

    11-29-2005, 11:52 PM
    • Member
      5 point Member
    • sbsummer
    • Member since 11-30-2005, 4:51 AM
    • Posts 1
    Same problem!!
  • Re: Dynamic Master Page Selection - Acessing ContentPlaceholders

    02-11-2007, 11:03 AM
    • Member
      136 point Member
    • Rauland
    • Member since 10-07-2006, 5:03 AM
    • Posts 74

    And almost a year and a half after,......

    ContentPlaceHolder myContent = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");

    Label

    myLabel = new Label();

    myLabel.Text =

    "Hello over a year after your question, and even though I don´t know whether this will answer it!!";

    myContent.Controls.Add(myLabel);

Page 1 of 1 (7 items)
Microsoft Communities