How to programmatically add stuff to contentPlaceH

2019-04-07 06:30发布

问题:

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.

回答1:

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.



回答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).



回答3:

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;


回答4:

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)