Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a Master Page from a user control?

I'm looking for a way to (preferably) strongly type a master page from a user control which is found in a content page that uses the master page.

Sadly, you can't use this in a user control:

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

I'm trying to access a property of the master page from the user control and would rather not have to pass the property from the master page to the content page to the user control because multiple content pages use the same user control. One change, one place whatnot.

like image 587
Bryan Denny Avatar asked Jan 06 '09 14:01

Bryan Denny


1 Answers

Try Page.Master.

Whatever whatev = (Whatever)Page.Master;

You'll have to make sure you add the proper using statements to the top of your file, or qualify the Master page type inline.

One potential gotcha is if this control is used by a different page whose master page is NOT the same type. This would only get caught at runtime.

like image 184
Dan Herbert Avatar answered Nov 06 '22 15:11

Dan Herbert