views:

97

answers:

3

Hi there!

I'm currently learning to use MasterPages and ContentPlaceHolders in ASP.NET 3.5 and C# - and right now, I'm desperately trying to edit the contents of an asp:Content-Control through my programming code.

Background is this; As soon as a Button is pressed, I want the asp:Content to be cleared and filled with dynamic generated content through the *.cs-File.

But I can't figure out how to access the asp:Content-Control, neither through its ID or Page.FindControl etc.

So, how can you edit the contents of a asp:Content-Control through your programming code?

A: 

Did you forget to add runat="server" to your asp:Content-Control?

Regards,

M.

Michel van Engelen
That was the first thing I checked, its set and correct...
ApoY2k
+1  A: 

You can put an asp:Panel into the ContentPlaceHolder and move the placeholder markup into it. This way you can get to the Panel container and manipulate its content.

Developer Art
Sounds like a fine workaround for me, I'll sure try this - still, the mystery remains :-/
ApoY2k
+2  A: 

You can access a ContentPlaceholder by using FindControl not on the page object, but on its master page.

System.Web.UI.MasterPage masterPage = this.Master;
ContentPlaceHolder pageContent;

pageContent = (ContentPlaceHolder)masterPage.FindControl("YourContentPlaceholderName");

I'd agree though that it's better to define a panel or placeholder within this block and access that directly, rather than try to modify the ContentPlaceholder

NickGPS
I don't want to modify the placeholder, but the content that fills a placeholder.Placeholder is defined in the MasterPage, but I'm trying to change a Conten-Control in a derived SubPage of that MasterPage.
ApoY2k
When you call Master.FindControl() it will return your page's instance of that placeholder, which will be your Content control.
NickGPS