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:
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.
public partial class WebUserControl : System.Web.UI.UserControl
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.
Remember Me
a@href@title, strike
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Andrew Robinson
E-mail