Atlantic BT: Our Thoughts

October 16 , 2009

Dynamic Body ID and Class properties on ASP​.NET Master Pages

by Jeremy Wiggins

Master pages are a great feature of ASP​.NET. However, they do have some drawbacks, one being they do not easily offer the flexibility of dynamic body ids and classes that our design team here needs.

Luckily a solution exists, and best of all it isn’t all that difficult to implement. The solution is built around the idea that child pages can access their master page, as well as any publicly exposed properties on the master. For example, in the Page_Load method of a child page, you can write:

MyMasterPage masterPage = Master as MyMasterPage;
masterPage.Property = “property value”;

This is not ideal though, because we are now committed to never changing our child’s master page; if we do, it will break our build. The correct implementation of this concept is to create an interface that all of our master pages will implement, then use that in our child pages’ Page_Load method:

IMasterPage masterPage = Master as IMasterPage;
masterPage.Property = “property value”;

We can still set our property this way, only now we can later change what master page we use, as long as the new one still implements the same interface. Good stuff. So good, that it puts us most of the way there towards dynamic body ids and classes.

To get these, we need a couple of properties on our interface:

public interface IMasterPage
{
     String BodyId { get; set; }
     String BodyClass { get; set; }
} 

Then, in the code-behind file for our actual master page:

public partial class MasterPagesDefault : MasterPage, IMasterPage
{

    private string _bodyId;
    private string _bodyClass;

    public string BodyId
    {
        get { return _bodyId; }
        set { _bodyId = value; }
    }

    public string BodyClass
    {
        get { return _bodyClass; }
        set { _bodyClass = value;}
    }
} 

Finally, in the master page’s body tag:

<body id=”<%= BodyId %>” class=”<%= BodyClass %>”>

Now, to set these dynamically, use the child page’s Page_Load method just like before:

IMasterPage masterPage = Master as IMasterPage;
if (masterPage != null)
{
    masterPage.BodyId = “index”;
    masterPage.BodyClass = “index two-col”;
} 

By using publicly accessible properties, we give child pages the ability to set values on their master page. With this, it is easy to create dynamic body classes and ids in .NET.

2 Responses to “Dynamic Body ID and Class properties on ASP​.NET Master Pages”

  1. Kiley S. says:

    Is there a complete download of this I can get a copy of? This is the exact problem I am working on but can’t hook it all together.

    many thank you’s.

  2. Hi Kiley,

    Glad you found the post helpful. I put together a quick sample app; you can download it here. Good luck!

Leave a Reply

In a Nutshell

Since 1998, Atlantic BT has been a full service web development company that offers the tools, resources and services to get your business moving. We focus on combining new ideas, specific requirements, and years of experience into high-quality, results-oriented web solutions for small to medium sized businesses. If you want the best website possible that generates real results, let's get started.

  • Toll Free: (866) 484-0921
  • Phone: (919) 518-0670
  • Tech Support: (866) 484-0921 (option 8)
  • Fax: (919) 719-0834
Atlantic Business Technologies, Inc.
8015 Creedmoor Road, Suite 101
Raleigh, North Carolina 27613
  • Raleigh Chamber of Commerce Member
  • Cary Chamber of Commerce Member
  • Pinnacle Business Award Winner
  • Triangle Business Journal's Top 40 Under 40
  • Yahoo Search Marketing Ambassador
  • Google Adwords Qualified Company