Saturday, August 25, 2007
« Quick ASP.NET ViewState Tip | Main | A System.Collections.Generic.Dictionary ... »
ASP.NET Tip: Occasionally I need to establish a line of communication between a User Control and my Master Page. Over the years, I have explored a few methods to accomplish this and seen more than a handful from others. The following method elegantly solves a number of problems for me and has become my standard coding practice.

First, a base class is needed for all Master Pages:

using System;

 

public abstract class BaseMaster : System.Web.UI.MasterPage

{

    public BaseMaster() { }

 

    public abstract void ShowMenu(bool visible);

    public abstract void SetStatusMessage(string message);

}

I define this class as abstract since it will never be instantiated directly. In addition, an abstract class allows us to include abstract methods and properties which in turn requires us to provides these methods on our actual Master Page.

My actual master page is coded as follows:

using System;

 

public partial class Main : BaseMaster

{

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

 

    public override void ShowMenu(bool visible)

    {

        MenuMain.Visible = visible;

    }

 

    public override void SetStatusMessage(string message)

    {

        LabelStatus.Text = message;

    }

}

Within a User Control, you can gain access to the Master Page via the Page.Master object. Using our base class (BaseMaster) you get a strongly typed object. You migh be tempted to cast the Page.Master object to the type of your Master Page but this limits you to a single Master Page. The base class method allows your site to implement multiple master pages and still access these common methods and properties.

using System;

 

public partial class WebUserControl : System.Web.UI.UserControl

{

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

 

    protected void ButtonUpdate_Click(object sender, EventArgs e)

    {

        BaseMaster master = Page.Master as BaseMaster;

 

        if (master == null)

            return;

 

        master.SetStatusMessage("Update Complete");

    }

}

One final consideration: your User Control should likely also derive from some type of base class. Within this base class provide matching properties and methods to those in your BaseMaster. This will simplify things for you by eliminating the need to recode the same methods and properties in each of your User Controls. If you are acessing these same methods from your Page, consider including methods in a page base class.

kick it on DotNetKicks.com   Saturday, August 25, 2007 8:28:37 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [2]  | 
Sunday, August 26, 2007 7:41:10 AM (Pacific Standard Time, UTC-08:00)
Nice code sample. Very slick.

Did you recently update to a new version of dasBlog?
Tuesday, October 23, 2007 8:41:38 AM (Pacific Standard Time, UTC-08:00)
Andy, couldn't help but cross link an article I wrote about this topic:

Subclassing Pages and Master Pages in ASP.NET 2.0
http://www.15seconds.com/issue/060413.htm
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview