Calling Properties on Master Pages

I still see quite a few questions on that deal with calling a property or method on the master page. There are two solutions to this problem:

  1. Use a Strongly Typed Master Page
  2. Use an interface.

Strongly-typed master pages is the way to do to deal with the problem. I personally experienced issues with strong-typing, and getting Visual Studio intellisense; therefore, I would often dump this in favor of an interface. To do this, create an interface:

public interface ISomeInterface
      string DataValue { get; set; }

Implement this in the code-behind file of the master page:

public class MyMaster : MasterPage, ISomeInterface

    public string DataValue
       get { return LabelControl.Text; }
       set { LabelControl.Text = value; }

Then you can cast all future reference to the page’s Master property to this interface:

public class TestPage : Page
    public void Page_Load(object sender, EventArgs e)
        ((ISomeInterface)Master).DataValue = "Some Value";

To make it easier, you can put this in a base page class:

public abstract class BasePage : Page
    new public ISomeInterface Master
       get { return (ISomeInterface)Master; }
       set { base.Master = value; }

And then all of your classes can implement this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s