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.
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.What normally happens is
ContentPlaceHolder
sContent
areas for you.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
And this went into code behind:
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)