How to programmatically add stuff to contentPlaceHolder?

大城市里の小女人 提交于 2019-12-04 05:27:07

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.

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

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;

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!