How to programmatically add stuff to contentPlaceHolder?

Ciprian Tomoiagă picture Ciprian Tomoiagă · May 8, 2012 · Viewed 26.1k times · Source

I have a master page and all of my pages are inheriting it. For formatting, I thought to place the content that differs from one page to another in a ContentPlaceHolder.

Now, how can I insert everything into that? Since I am planning to populate the ContentPlaceHolder with stuff from a database I suppose I will have to do it programmatically.

  1. How can I add controls to ContentPlace Holder? I checked other answers, but I cannot access it by its ID.

  2. Should I use multiple ContentPlaceHolders from the beginning? Let's say I want to put movies. Should there be only one with all the images and descriptions and ratings, ore one ContentPlaceHolder for each thing?

I am opened to other solutions, as I have no experience with ASP.

Answer

MikeSmithDev picture MikeSmithDev · Oct 8, 2013

Old question... but I just ran into this issue and this was the #1 post that kept coming up on Google, so figure I'd add my answer since the others didn't work in my case.

Here is how I did it when a regular <asp:Content wouldn't work (though in normal use, the answer @JayC is how you do it):

MasterPage has this ContentPlaceHolder:

<asp:ContentPlaceHolder ID="ScriptsPlace" runat="server"></asp:ContentPlaceHolder>

Had to dynamically add some JavaScript from a User Control. Trying to use the ContentPlaceHolder directly gives this error:

Parser Error Message: Content controls have to be top-level controls in a content page or a nested master page that references a master page.

So I wanted to add the script from the code-behind. Here is the Page Load for the .ascx file:

protected void Page_Load(object sender, EventArgs e)
{
    ContentPlaceHolder c = Page.Master.FindControl("ScriptsPlace") as ContentPlaceHolder;
    if (c != null)
    {
        LiteralControl l = new LiteralControl();
        l.Text="<script type=\"text/javascript\">$(document).ready(function () {js stuff;});</script>";
        c.Controls.Add(l);
    }
}

UPDATE: So it turns out I had to use this in more places than I expected, and ended up using a way that was much more flexible / readable. In the user control itself, I just wrapped the javascript and anything else that needed to be moved with a regular div.

<div id="_jsDiv" runat="server">
    $(document).ready(function() {
         //js stuff
    });
    Other server controls or HTML junk
</div>

And then the code behind will find that div, and then move it into the ContentPlaceHolder.

protected void Page_Load(object sender, EventArgs e)
{
    ContentPlaceHolder c = Page.Master.FindControl("ScriptsPlace") as ContentPlaceHolder;
    HtmlGenericCOntrol jsDiv = this.FindControl("_jsDiv") as HtmlGenericControl;
    if (c != null && jsDiv != null)
    {
        c.Controls.Add(jsDiv);
    }
}

I actually put this code in a custom user control, and I just have my regular user controls inherit from the custom user control, so once I wrap the javascript/etc with a <div id="_jsDiv" runat="server">, the custom user control takes care of the rest and I don't have to do anything in the code behind of the user control.