How do you cast from Page.Master to a specific master page in ASP.NET - asp.net-2.0

I have a BasePage which inherits from System.Web.UI.Page, and every page that inherits the BasePage will have the same master page.
How do I cast the Page.Master of the BasePage to the specific master page so I can access properties on it?

A better way is to add the MasterType property to the pages that use that master. Then you can simply access the master page properties through the page object.
<%# MasterType VirtualPath="~/site.master" %>
You just use this in your code:
this.Master.propertyName
To access the property of the master page for the current page.

Overriden Master can't be done (its not Virtual), and masking it with new causes an issue with the page class not being able to get its master, so the best thing to do is a second property.
Something like:
public CustomMasterPage MasterPage
{
get { return this.Master as CustomMasterPage; }
}
In your BasePage class.

In VB.Net
MasterPageVariable = Ctype(page.MasterPage, MasterPageClass)

Related

access master page from ascx control

I have a public property in a master page. I want to access this property from an ascx user control.
My master page is named master_public and it's in a namespace called "master".
So I tried writing:
dim m=ctype(page.master,master.master_public)
dim foobar=m.foobar
The intellisense says that master.master_public doesn't exist.
I tried removing the namespace from the master page. Still no luck.
The master page is declared "partial public class". But this name doesn't seem to be recognized.
Answers here Accessing Master page control in ascx file seem to imply that this should just ... work. Is there some attribute or setting or something I have to include to make master pages accessible as class types?
Add the following on top of your form (.aspx):
<%# MasterType TypeName="master.master_public" %>
The above directive will expose public members of the masterpage to the form. To access your property from the form, simply reference as below:
Me.Master.YourProperty
Therefore, in order to access the masterpage public property from user controls added to the form, just cast the master page object :
CType(Me.Page.Master, master.master_public).YourProperty

Master page inheritence dilemma

Bit of a confusion for me here. This is ASP.NET 4.0. Looking at the markup, it appears that Default.aspx inherits from Site.Master page. But looking at the class definition of Default.aspx (which is named _Default), it inherits from Page class and not SiteMaster.
Now I need to share a few functions across multiple inherited pages and was looking to add them to SiteMaster class so that they would be available in all inherited pages. Can I use SiteMaster class for my purpose, or should I add an independent module to my project and add all my functions to that?
Figured it out. For anyone else looking for a solution, all you need to do is to add the following directive to your markup:
<%# MasterType VirtualPath="~/Site.Master" %>
You can add this line to the top of ASPX file, just under the <%# Page ... %> line. Once added, simply rebuild your project. Now all the public members of your master page class will become available through the strongly-typed member named Master. For example, you can now use the following code to access a public function called MyFunc() defined in the master page class:
this.Master.MyFunc(); //C#
Me.Master.MyFunc() 'VB.NET
MasterType directive actually adds a shadowing member named Master that is strongly-typed as SiteMaster class (or whatever master page class you have specified) when spitting the code-behind class. This new member hides the base class member (whose type is the generic MasterPage class) and thus allows you to access your master page members directly. The property is defined in the .aspx.Designer.cs/vb file like this:
Public Shadows ReadOnly Property Master() As YourProjectNamespace.SiteMaster
Get
Return CType(MyBase.Master, TagTrakkerOnline2.SiteMaster)
End Get
End Property

How does one get a code-behind file to recognize type of a user control?

I have a custom control in my mater page that represents a menu, let's call it CustomMenu. The control code files are located in a non special directory.
I want to give each page that uses that master page the ability to access the control and, using a public property, tell the control whether or not it should render itself.
I have a public property on the control to do that, and I know I can get the control by referencing Page.Master.FindControl('IdOfControlIwant');
The problem I'm having is that I can't seem to get the control Type recognized by the compiler, so when I do find the menu control, I can't actually work with it. The only way I can get the code behind to recognize the type is to register it in the ascx file, and then add at least one control to the page, which is undesirable.
Thoughts?
You have to combine what both Jacob and dzendras have posted. Add the MasterType directive to your content page's aspx file:
<%# MasterType VirtualPath="~/your.master" %>
And in the master page create a property:
public CustomMenu MyCustomMenu {get{ return myCustomMenu;}}
Where myCustomeMenu is the ID of the Usercontrol in your masterpage.
You should now be able to reference the usercontrol from a content page. So, if the CustomMenu usercontrol had a property called SelectedItem, you should be able to access it like this:
public void Page_Load(object o, EventArgs e)
{
Master.MyCustomMenu.SelectedItem = 1;
}
Use the MasterType directive in your pages:
<%# MasterType VirtualPath="~/your.master" %>
That will strongly type your Master page reference, so you should be able to add properties that can be accessed by the pages.
Make a property of your MasterPage class:
bool IsCustomMenuVisible {set{ CustomMenu.Visible = value;}}
And use it wherever you like.

Getting instance of content page within the master page

How can I get the instance of the content page from the maste page?
I need it for that:
All my content pages derive from BasePage class (and BasePage derives from System.Web.UI.Page), the BasePage has a property Index.
The derived page set it's value so that master page can read id and apply special CSS to corresponded menu item that is located on the master page.
Just cast the current page from your MasterPage as your BasePage:
BasePage currentPage = (BasePage)this.Page;
int index = currentPage.Index;
SetMenuIndex(index);
You could define a public function to change the css in the MasterPage that you can call from derived pages on index change.
DirectCast(Me.Page.Master, YourMasterPageType).ChangeCss()

How to access an enum in master page

Can we access an enum which has been defined in the master page. I read about the master page on my own blog.
If you mean an actual enum, defining it elsewhere is a better option, but the same casting below works to get at it as well.
If you want to access the property that is an enum then cast the Master property of your page to your master page's type. Like this:
protected void override OnLoad(EventArgs e)
{
((MyMasterPagesType)Master).MyEnumProperty = MyEnum.Value;
}
Edit:
Almost forgot, you can also have the Master property on your page already behave as this type by using the #MasterType directive in your aspx markup.
<%# MasterType VirtualPath="~/masters/SourcePage.master”" %>

Resources