How to programmatically add stuff to contentPlaceHolder?
Asked Answered
D

4

7

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.

Drab answered 7/5, 2012 at 23:16 Comment(0)
D
12

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.

Domela answered 8/10, 2013 at 19:30 Comment(1)
This works brilliantly. It could even be extended to include a control parameter for the placeholder name. I used this solution when I needed to programmatically place a javascript file in a particular sequence/position, and not in the header.Tana
B
2

What normally happens is

  1. you set up your master pages with the proper html and ContentPlaceHolders
  2. you create pages based off that master page. If you use Visual Studio, and tell it to create a new page based upon a existing Master page, it will add the Content areas for you.
  3. you add things to the Content areas in the newly created page.

If you want to dynamically add controls to the master (or any) page, you could add controls to any existing control. If it shouldn't be wrapped in any way, just add a Placeholder (it is an asp.net control).

Beyrouth answered 7/5, 2012 at 23:30 Comment(5)
I am sorry, that doesn't really solve my problem. I need some code for programatically putting, let's say, some text, into that Content area, because I can't access it from the CodeBehind the web page. I have default.aspx page and the ContentPlaceHolder with ID = content1. In default.aspx.cs I can't "see" content1.Obryan
I see. Is there any reason you cannot put a placeholder in your page's Content area? You can certainly add things to that:Beyrouth
For example: Label lbl =new Label(); lbl.Text="something"; placeHolder.Controls.Add(lbl);Beyrouth
No, I could use a placeHolder. That would be ok, I guess. But now I realised that my problem is far more complex than this. Should I ask a different question? I want to integrate a different content in that area, depending on my querrylink. Something like using import in PHP. If, for example, I have ?category=1 then in that area would be category description and products or, if I have ?product=xx then in that area would pe an image, a description and a price. What would be the easiest method to do that?Obryan
I'd be tempted to separate the types of things to show in there as separate user controls and then dynamically load the proper control as needed (as in, say your page's Page_Init method, var control = Page.LoadControl("~/path/to/TheNeededUserControl.ascx"); then set up the control and add it to the placeholder's Controls collection) but that might get a little harry depending upon how your project is set up. It might be even easier just to have separate pages, each doing what it needs to do, and then UrRewrite them to the same base Url in IIS7 (if using that). You have many options.Beyrouth
P
1

I did like this

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Literal ID="jsstuff" runat="server"></asp:Literal>
</asp:Content>

And this went into code behind:

string stuff =  @"<script type=""text/javascript"">
                                        var searchBox = 0;
                                         var currentCountry = '';
                                         </script>";
                    jsstuff.Text = stuff;
Pancratium answered 6/7, 2012 at 2:20 Comment(0)
K
0

If the namespace for content Page and Master page is not same then the content page control not accessible in Codebehind in content page.

Also, check your designer files. if the control not listed in designer file then delete the file and recreate (project->convert to web application)

Killoran answered 17/12, 2016 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.