Solved

How To Cast A Master Page?

Posted on 2009-05-05
7
275 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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