Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1973
  • Last Modified:

Dynamically adding a ContentPlaceHolder

I have a master page that uses a user control to define most of its layout.  Inside the nesting of controls in the user control, I need to add a ContentPlaceHolder.  But the ContentPlaceHolder control can only be used inside .master files.

So I am trying to add the ContentPlaceHolder dynamically.  I can get this to work but have to use a hack to prevent a compiler error.  If I actually code and comment out the ContentPlaceHolder in the  html of the master  page, it prevents the compiler error Cannot find ContentPlaceHolder MasterPageMain in the master page..., but what is the right way to do this?

Here is the code in the master page that I am using to dynamically add the ContentPlaceHolder:

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim cph As ContentPlaceHolder
        cph = New ContentPlaceHolder
        cph.ID = "MasterPageMain"
    End Sub

MsgsControl1 is the user control which defines the layout.  This user control is defined as a property of the base class for the master page. The user control itself also is defined as a base class with the property Placeholder.

 This code works and puts the user control where it should go.  But it doesnt happen early enough in the compilation to prevent the compilation error.  I tried using a constructor which happens earlier than Page_Init but this code couldnt resolve the reference to my base class.  Does anyone know how to do this correctly or to get around the compilation error?

Thanks very much.
  • 4
  • 4
1 Solution
>Inside the nesting of controls in the user control, I need to add a ContentPlaceHolder.  But the ContentPlaceHolder control can only be used inside .master files. <

Why specifically a ContentPlaceHolder?  Why not a PlaceHolder control?

Dynamic controls have some pros and cons.  You can't reference a dynamic control the same way you would one added at design time:

Working with Dynamically Created Controls
cbearden11Author Commented:
Thanks, for tht idea.  But when I try it, how do I tie my content page back to the PlaceHolder?

If I use a PlaceHolder instead of a ContentPlaceHolder can I tie the javascript + server controls to the PlaceHolder in the same way that I would tie them to a ContentPlaceHolder?  The content page refers back to the ContentPlaceHolder using the <asp:Content.. >  Tag.  Can I do this similarly with PlaceHolders?
javascript can be registered from code behind as you normally would with

ClientScript.RegisterClientScriptInclude("Key", "Scripts/myScriptFile.js")


ClientScript.RegisterClientScriptBlock(this.GetType(), "InitScript", scriptLiteral);

at the page or usercontrol level.

The server controls can be added to the placeholder (that's the whole point) and referenced by their clientID for the javascript or use PlaceHolder.FindControl for server side and cast to the correct type:

Control..::.FindControl Method (String)


Dim MyText as TextBox = DirectCast(PlaceHolder.FindControl("MyTextID"), TextBox)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

cbearden11Author Commented:
Ok, thanks.  But in the markup of my content page I can contain all of my javascript plus server controls inside a single content tag like this:
<asp:Content ID="MainContent" ContentPlaceHolderID="MasterPageMain" runat="server">
   javascript + server controls go here

Is there a similar way to do this with the PlaceHolder Control or do I have to add everything individually in the code behind like you explained above?
If you are declaring the controls in the markup, then you don't really need a placeholder... You can just add them to your usercontrol or use a panel.  The placeholder is specifically designed for dynamic controls added at run time.
cbearden11Author Commented:
I am using Master Pages and Content pages and so I need to have ContentPlaceHolders in the master page to identify the places that the Content pages controls will reside in. So PlaceHolders won't work here.  So I need to get the ContentPlace Holder working correctly.
The compile error is occurring in the content pages right?  Because you can't create asp:Content tags that reference elements that are not created through declaration.

Really Dynamic Master Pages

You will see that this essentially means that all the content must be added dynamically once you go down this path.  I don't think that's what you really want, is it?

A redesign of your master page, without the usercontrol would probably be a better fit.  You can use multiple master pages, nested master pages, or customize your content using multiple ContentPlaceHolder controls on your master page and setting some of them visible = false when you don't need them.
cbearden11Author Commented:
As it turns out, a nested master page turned out to be the solution.  If I use a nested master page to pull in the markup, rather than a user control, then I can refer to the ContentPlaceHolder with no problem.  Thanks very much for your help.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now