Master Page Properties

Just a quick note for those of you who like to use Master pages in ASP.Net.

Have you ever had something on the Master page that you wished you could change from the child page? Well, for those of you who have actually done this, one way is to get the Master page from the Page object on the backend, search it for a control that you are after, cast that control to the right type, and … you get the point. Long and tedius.

Try this instead: Create a property on the master page that reflects the thing you want to be able to change. On the child page, at the top add this directive:

<%@ MasterType VirtualPath="~/Default.master" %>  

where Default.Master is the full path to the master page that the page is using. Then in the child’s code behind, you can do something like:

Master.PropertyName = PropertyValue;  

Should save a lot of time. Hope this is useful to someone.