Dynamically adding a ContentPlaceHolder

Posted on 2009-02-12
Last Modified: 2012-05-06
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.
Question by:cbearden11
    LVL 38

    Expert Comment

    >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

    Author Comment

    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?
    LVL 38

    Expert Comment

    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)

    Author Comment

    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?
    LVL 38

    Expert Comment

    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.

    Author Comment

    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.
    LVL 38

    Accepted Solution

    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.

    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now