views:

23

answers:

2

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;
        }
    }
}
+1  A: 

Have you tried adding the MasterType directive to your content page? Like so:

<%@ MasterType TypeName="[Fully qualified class of the master page]" %>

I recommend doing that anyway. Let's see if that helps you...

See http://msdn.microsoft.com/en-us/library/ms228274.aspx and http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx

md1337
A: 

This works for me now...

public class PageBase : Page
{
    public string BodyCssClass
    {
        get;
        set;
    }

    protected override void OnPreInit(EventArgs e)
    {
        MasterPageBase mpbCurrent = this.Master as MasterPageBase;
        mpbCurrent.BodyCssClass = BodyCssClass;

        base.OnLoadComplete(e);
    }
}
Naeem Sarfraz