Unable to cast object of type 'ASP.platform_epp_portfolio_eppmasterpage_master' to ty

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
I'm getting the above error when running through my code. I know at desgin time what the problem is, but at run time I'm changing the MasterPage to use with a specific aspx page in the Page_PreInit event. If I could solve the design time compile error, I'm hoping it will fix the run-time error.

I have code in my code-behind file that looks like the following:

Visual Basic:
Public WithEvents objMaster As MasterPage

'Get a reference to the master page
objMaster = DirectCast(Me.Master, MasterPage)

'Add the section and get a treeview back
objMaster.AddCustomSection(2, "Folders")

Protected Sub Page_PreInit1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

If Session.Item("EPPMasterPageFile") IsNot Nothing Then
    Me.MasterPageFile = Convert.ToString(Session.Item("EPPMasterPageFile"))
Else
    Me.MasterPageFile = "~/MasterPage.master"
End If

End Sub

The boldfaced line above has a design time compile error in it.stating that "AddCustomeSection is not a member of 'System.Web.UI.MasterPage".

When I have the MasterPageFile property of the aspx page at design time set to a master page (which is contained inside the Sessin variable), "MasterPageFile="~/Platform/EPP/Portfolio/EPPMasterPage.master", that's when the compile error occurs.

I have the following section inside the above masterpage:

Visual Basic:
Public Sub AddCustomSection(ByVal Index As Integer, ByVal Title As String)

Dim objCustomSection As New CustomSection
objCustomSection.Index = Index
objCustomSection.Title = Title

objCustomSections.Add(objCustomSection)
End Sub

As you can see, the sub is public and should be able to be seen by my code behind file with the intellisense.

Now, bearing the above in mind, when I change the MasterPageFile property of the aspx page at design time to the main master page (on the root of the website), with the same code above (the main one has the Sub in it as well), I do not get a design time compile error!

The application runs and changes the master pages successfully with no problem, except when I click on a link that needs the code above (inside the masterpage, which is not on the root).

Note that these masterpages are not related, and they are not in a parent/child relationship.

Does anybody know what the problem is?
 
Last edited by a moderator:
The builtin class MasterPage is a class that other MasterPages inherit from and does not contain a definition for your AddCustomSection method.

Rather than casting the MaterPage property to a MasterPage (which it already is) like you are doing with
Visual Basic:
objMaster = DirectCast(Me.Master, MasterPage)
you will need to declare it and cast it to the correct type
Visual Basic:
Dim objMaster as EPPMasterPage    'Or whatever the name of the class for your master page is.
objMaster = DirectCast(Me.Master, EPPMasterPage)
 
Back
Top