How To Cast A Master Page?

I have a MasterPage designed called "Template_RegularPage"...

In some back code in a custom class I have a reference to Me.Master...  I am trying to access a method inside the MasterPage, but I need to cast Me.Master to the proper type first.  Unfortunatly... CType(Me.Master, Template_RegularPage) doesn't work...  How do I cast the MasterPage in VB Code?  Is the MasterPage I created nested in a certain namespace?
Ben SantiardoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
wht1986Connect With a Mentor Commented:
Dont give up, there is always a way.  Try this

In the App_Code folder, create (2) new class files

'==============================
'App_Code/MyBaseMaster.vb
Imports Microsoft.VisualBasic

Public MustInherit Class MyBaseMaster
    Inherits System.Web.UI.MasterPage

    ' including this stub to show how to define a function without implementing it
    ' we will define it on the real master page
    Public MustOverride Function GrabSomeRealValue() As String

End Class

'==============================
'App_Code/MyBasePage.vb
Imports Microsoft.VisualBasic

Public MustInherit Class MyBasePage
    Inherits System.Web.UI.Page

    Public ReadOnly Property MyBaseMaster() As MyBaseMaster
        Get
            Return Me.Master
        End Get
    End Property
End Class

'==============================

Then make all your pages inherit from MyBasePage
and your master pages inherit from MyBaseMaster

like (assuming the Template_RegularPage is still in the app_files folder)

'======================================
'App_Files/Template_RegularPage.master
Partial Class App_Files_Template_RegularPage
    Inherits MyBaseMaster

    ' this is where we deine what our function really does
    Public Overrides Function GrabSomeRealValue() As String
        Return "Hello World"
    End Function

End Class

'======================================
and for the page
'======================================

Partial Class _Default
    Inherits MyBasePage
End Class

'======================================

now on your page you can get the reference to your master page by
Me.MyBaseMaster

put any methods you need in the MyBaseMaster class like i showed above, you can then call it them like this from the page

Dim s As String = Me.MyBaseMaster.GrabSomeRealValue()
0
 
wht1986Commented:
try
        Dim masterReference As Template_RegularPage = Me.Master
then you can use masterReference  to access any public methods or properties you have defined
0
 
Ben SantiardoAuthor Commented:
I can't because I can't find "Template_RegularPage" anywhere using Intellisence.  I tried what you wrote above and got the following error:
Type 'Template_RegularPage' is not defined.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Ben SantiardoAuthor Commented:
IDK if this makes a difference but, the code I am trying to write this in belongs to the "App_Code" directory and the MasterPage is located in a directory called "ASP_Files"  Both directories are off the same root.
I don't see how that would make a difference but just in case.
0
 
wht1986Commented:
I am guessing 2 things.

(1) When I create a master page called Template_RegularPage in the ASP_Files folder, by default the class created is really called ASP_Files_Template_RegularPage. Is that the case? you can check by looking at the Template_RegularPage.master.vb file and looking for the definition like
Partial Class ASP_Files_Template_RegularPage

(2) You are being bitten by the evil app_code folder. I am guessing you have a class in the app_code folder that inherits from System.Web.UI.Page class.  And in this class, you are trying to reference the Me.Master property.  Well it has to do with how web sites versus web application projects are built.  Solution move that class out of App_Code.  Then one of the following should work depending on how the master page is defined

Dim masterReference As Template_RegularPage = Me.Master
Dim masterReference As ASP_Files_Template_RegularPage = Me.Master
0
 
Ben SantiardoAuthor Commented:
Hi and thanks for responding.  Guess #1 is correct.  As for #2, it appears you are absolutely correct on that as well.  I started moving files around and began to see behavioral changes in the UI that allowed me access to code which before it didn't.  Problem is, now that I moved the DerivedPageCode file, all my pages that Inherited from it are now complaining that the file is not found.  So It looks like I am stuck in a Catch-22...
I can't access the MasterPage methods from Class Code in the App_Code folder and I can't access the Class Code in the ASP_Files folder.  
ARG!!!!
0
 
Ben SantiardoAuthor Commented:
OMG!  Your a lifesaver!  That was exactly what I needed! Thank you so much!

This way of doing things even allowed me to refactor and shrink a good portion of code! Again, thank you soooo much!

-Ben
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.