How to programmatically add material to contentPlaceHolder?

I have a homepage and all my pages inherit it. For formatting, I thought of putting content that is different from one page to another in a ContentPlaceHolder.

Now, how can I insert everything into this? Since I plan to populate the ContentPlaceHolder with material from the database, I suppose I will have to do this programmatically.

  1. How to add controls to ContentPlace holder? I checked other answers , but cannot access it by its identifier.

  2. Should I use multiple ContentPlaceHolders from the start? Let's say I want to make films. Should there be only one with all images, descriptions and ratings, or one ContentPlaceHolder for each item?

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

+6
source share
4 answers

An old question ... but I just ran into this problem, and it was message # 1, which continued to appear on Google, so I will add my answer as others did not work in my case.

This is how I did it when the regular <asp:Content didn't work (although with normal use, @JayC's answer is how you do it):

MasterPage has this ContentPlaceHolder :

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

I had to dynamically add JavaScript from User Control. Attempting to use ContentPlaceHolder directly gives this error:

Parser error message: Content controls must be top-level items on the content page or in the nested master page that links to the master page

So, I wanted to add a script from the code. Here is the Download page 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:. Turns out I had to use this in more places than I expected, and ended up using a method that was much more flexible / readable. In the user element itself, I just wrapped javascript and everything else that needed to be moved using 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 the div, and then move it to 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 really put this code in a user control, and I have regular user controls inherited from the user control, so as soon as I finish javascript / etc with <div id="_jsDiv" runat="server"> , the user the control will take care of the rest, and I don’t need to do anything in the control code of the user control.

+11
source

What usually happens is

  • you customize your master pages with the appropriate html and ContentPlaceHolder s
  • You create pages based on this main page. If you use Visual Studio and tell him to create a new page based on an existing master page, it will add Content areas to you.
  • you add things to the content area on the newly created page.

If you want to dynamically add controls to the main (or any) page, you can add controls to any existing control. If this should not be wrapped in any way, just add a Placeholder (this is an asp.net control).

+2
source

I liked it

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

And this went into the code behind:

 string stuff = @"<script type=""text/javascript""> var searchBox = 0; var currentCountry = ''; </script>"; jsstuff.Text = stuff; 
+1
source

If the namespace for the content page and the main page is not the same, then the content page control is not available in Codebehind on the content page.

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

0
source

Source: https://habr.com/ru/post/915138/


All Articles