Set ASP:ContentPlaceHolder Content programmatically

Anders picture Anders · Nov 14, 2008 · Viewed 7.7k times · Source

What is the easiest way to set the contents of an <asp:ContentPlaceHolder> programmatically? I imagine ill have to do a Master.FindControl call?

Answer

tvanfosson picture tvanfosson · Nov 15, 2008

If your page inherits from a MasterPage, then you should have an asp:Content control on your page with some id, like so:

<asp:Content runat="server" ID="myContent" ContentPlaceHolderID="masterContent">
</asp:Content>

You should be able to reference this in your codebehind and add whatever you want to it.

public void Page_Load( object sender, EventArgs e )
{
    HtmlContainerControl div = new HtmlGenericControl( "DIV" );
    div.innerHTML = "....whatever...";
    myContent.Controls.Clear();
    myContent.Controls.Add(div);
}