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.
-
How can I add controls to ContentPlace Holder?
I checked other answers, but I cannot access it by its ID. -
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.
Best Answer
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
:Had to dynamically add some JavaScript from a User Control. Trying to use the
ContentPlaceHolder
directly gives this error:So I wanted to add the script from the code-behind. Here is the Page Load for the
.ascx
file: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
.And then the code behind will find that div, and then move it into the
ContentPlaceHolder
.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.