Solved

How To Cast A Master Page?

Posted on 2009-05-05
7
273 Views
Last Modified: 2013-11-07
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?
0
Comment
Question by:Ben Santiardo
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:wht1986
ID: 24307999
try
        Dim masterReference As Template_RegularPage = Me.Master
then you can use masterReference  to access any public methods or properties you have defined
0
 

Author Comment

by:Ben Santiardo
ID: 24308041
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
 

Author Comment

by:Ben Santiardo
ID: 24308080
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 12

Expert Comment

by:wht1986
ID: 24308503
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
 

Author Comment

by:Ben Santiardo
ID: 24313669
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
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 total points
ID: 24314312
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
 

Author Closing Comment

by:Ben Santiardo
ID: 31578164
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now