ASP.NET Content Web Form - content from placeholder disappears
- by Naeem Sarfraz
I'm attempting to set a class on the body tag in my asp.net site which uses a master page and content web forms. I simply want to be able to do this by adding a bodycssclass property (see below) to the content web form page directive.
It works through the solution below but when i attempt to view Default.aspx the Content1 control loses its content. Any ideas why?
Here is how I'm doing it. I have a master page with the following content:
<%@ Master Language="C#" ... %>
<html><head>...</head>
<body id=ctlBody runat=server>
 <asp:ContentPlaceHolder ID="cphMain" runat="server" />
</body>
</html>
it's code behind looks like:
public partial class Site : MasterPageBase
{
    public override string BodyCssClass
    {
        get { return ctlBody.Attributes["class"]; }
        set { ctlBody.Attributes["class"] = value; }
    }
}
it inherits from:
public abstract class MasterPageBase : MasterPage
{
    public abstract string BodyCssClass
    {
        get;
        set;
    }
}
my default.aspx is defined as:
<%@ Page Title="..." [master page definition etc..] bodycssclass="home" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cphMain" runat="server">
  Some content
</asp:Content>
the code behind for this file looks like: 
public partial class Default : PageBase { ... }
and it inherits from :
public class PageBase : Page
{
    public string BodyCssClass
    {
        get
        {
            MasterPageBase mpbCurrent = this.Master as MasterPageBase;
            return mpbCurrent.BodyCssClass;
        }
        set
        {
            MasterPageBase mpbCurrent = this.Master as MasterPageBase;
            mpbCurrent.BodyCssClass = value;
        }
    }
}