tags:

views:

294

answers:

2

I'm using a asp.net web application project, and I cannot access any asp:content controls programmaticly. I'm trying to update its contentplaceholderID in code behind. Is this possible?

Thanks

A: 

No you can't change the contentPlaceHolderId in your content page. The reason is that contentplaceholder are set in Master page and then you reference them in there to say in which contentplaceholder your content will go.

The Content controls get left behind and don’t exist in the control hierarchy. This ASP.net thread here gives a way of how you can update it another way though

TStamper
What are you actually trying to do because to call a web control in asp.net for any control it is the same
TStamper
im trying to set the value of the contentplaceholderID property on the content control from a web.config appsetting.
Shawn Simon
the code you posted doesn't work
Shawn Simon
@Shawn-just updated answer, didn't know that the contentplaceholder was different
TStamper
+1  A: 

Although you probably cannot change ContentPlaceHolderId of your asp:Contents, you can control the content of ContentPlaceHolders directly: access the master page from the page through its Master property and then use the FindControl function to find the ContentPlaceHolder on the master page. After that, edit its content through the Controls property.

var masterContent = Master.FindControl("largeContent") as ContentPlaceHolder;
masterContent.Controls.Add(new Literal { Text = "Hello, world!" });

You could probably use this 'hack' to add an asp:Panel control to any ContentPlaceHolder you want so this might emulate the functionality you wanted.

gius