Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted (edited)

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:

 

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:

 

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?

Edited by PlausiblyDamp

Thanks,

 

Bill Yeager (MCP, BCIP)

Microsoft Certified Professional

Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer

YeagerTech Consulting, Inc.

  • Administrators
Posted

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

objMaster = DirectCast(Me.Master, MasterPage)

you will need to declare it and cast it to the correct type

Dim objMaster as EPPMasterPage    'Or whatever the name of the class for your master page is.
objMaster = DirectCast(Me.Master, EPPMasterPage)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
Thanks. that was it....

Thanks,

 

Bill Yeager (MCP, BCIP)

Microsoft Certified Professional

Brainbench Certified Internet Professional, .Net Programmer, Computer Programmer

YeagerTech Consulting, Inc.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...