Typed Masterpage reference

ASP.NET master pages are pretty handy and here is a simple tip to make them even easier to use.

Using the MasterType reference in your content page allows you to refer to your master page properties without having to cast it to your master page type everytime.

For example...

<%@ MasterType TypeName="myMasterType" %>

allows me to write...

this.Master.myProperty = "value";

rather than...

((myMasterType)this.Master).myProperty = "value";

Which saves you a little typing and provides intellisense

Author Dave Howard

I have been involved in IT development for the last 10 years - a lot of it around desktop applications and telecoms.


phayman said:

Nice little find, thanks Dave.

25/Sep/2006 12:39 PM

jaycat said:

also in the mastertype adding the following aslo works: virtualpath="~/MasterPage.master"

26/Sep/2006 22:17 PM

phayman said:

Just have to say again... excellent find, I've been using this loads, makes life soooo much easier!!

29/Sep/2006 21:34 PM

Paul said:

yet again I find myself referring to this article

17/Jan/2008 23:11 PM

Ben van Enckevort said:

Thank you, SO much. I have been trying to find this answer for too long

23/Sep/2008 18:44 PM

Add Comment


Your comment has been received and will be shown once it passes moderation.