How to properly access controls in the Master Page

One of the great new features in ASP.NET 2.0 has been the ability to use master pages to layout the common elements of the web site.  This certainly works better than top and bottom include files or top and bottom Web User Controls.  But at least with the user controls, if we wanted to access a control within it, the method to do so was pretty straight forward.  All you had to do was expose the control as a public property.  Or, better, create a pass through property on the control that sets or retrieves the inner control’s property.  By doing this, you could turn a side bar element on or off (as an example.)

While you can expose elements of a master page in a similar manner, anyone who’s done this knows there are a few gotchas along the way.  I’m going to tell you how to avoid the gotchas and you’ll have the added benefit of being able to access the controls on the master page even if you decide to use a different master page without having to change any code other than the master page directive at the top of the aspx page.

Now, just to bring everyone on to the same page, a master page is just a web user control.  So, we can expose controls or properties of those controls the same way and access them from the inner page’s Master property.  So, for starters, let’s say we create a master page with a table:

Header
Content Area SideBar
Footer

We’ll name the master page, MainMasterPage because MasterPage is the name of the Class that represents the master page and we’ll get ourselves into all kinds of confusion explaining this if I don’t have unique names for things.

OK.  So, the next thing we want to do is make it so that we can conditionally hide the SideBar from a main page.  So, to do that, we’ll want to make the TD surrounding the SideBar a runat=”Server” control and give it an ID.  Might as well call it _sideBar.

Next, we’ll want to be able to turn the sidebar off.  So, we’ll create a property in our MainMasterPage’s codebehind file called, SideBar and make it boolean:

public bool SideBar     

{     

 get { return _sideBar.Visible; }     

 set { _sideBar.Visible = value; }     

}

So, now comes the fun part.  Accessing this property from the codebehind file of or aspx page.

As I mentioned before, the Page object has a Master property hanging off of it, but if we just access it directly using Page.Master, you’ll see that there is no SideBar property hanging off of it.  That’s because we need to cast the Master property to a MainMasterPage before we can see the SideBar property.  So, our final code to turn off the side bar from within our aspx page’s codebehind will look like:

 ((MainMasterPage)(Page.Master)).SideBar = false;

If you did everything correctly, you should now have a side bar that goes away.  This code works 99% of the time.  But, I’ve run into issues where I’ve made a change to the code and recompiled and I get an error message saying that MainMasterPage can’t be found even though all of the code is correct.  No errors in the MasterPageFile or in the ASPX file.  Still, it won’t build.

The only reasonable explanation for this is that .NET is doing some sort of inconsistent magic behind the scene that works most of the time but causes this error.  Something like maybe changing the class name of MainMasterPage, or the namespace it lives in.

However I have found that you can completely eliminate this error by creating an interface the the defintions of your properties you’ve defined in your master page, implementing the interface on your master page, and then casting Page.Master to the interface type instead of the MainMasterPage type.

So, create the interface:

public interface IMainMasterPage      

{  public bool SideBar { get;set;} }

Implement it on MainMasterPage:

public partial class MainMasterPage : System.Web.UI.MasterPage,    

 IMainMasterPage    

{     

 protected void Page_Load(object sender, EventArgs e) { }     

 public bool SideBar     

 {     

 get { return _sideBar.Visible; }    

 set { _sideBar.Visible = value; }     

 }     

}

And, change our cast:

 ((IMainMasterPage)(Page.Master)).SideBar = false;

You now have the added advantage of being able to create another master page, applying the same interface and implementing the SideBar property on it and you can either change the master page in design view or write code to change it dynamically and the code will still work.  Without the interface, this change would require you to change the codebehind in each page that accessed the SideBar property.

Technorati Tags:

,

,

del.icio.us Tags:

,

,

Related Post

Comments are closed.