Try our conversational search powered by Generative AI!

Unable to cast object (after upgrade to R2)

Vote:
 

After upgrading to R2 I'm getting errors in PageList when casting from PageBase to custom PageType (created with PageTypeBuilder).

<a href="<%# ((Article)Container.DataItem).LinkURL %>">
<%# shortenIngress(((Article)Container.DataItem).Ingress, 400)%>
</a>

 

[InvalidCastException: Unable to cast object of type 'EPiServer.Core.PageData' to type 'Site.Web.Templates.Public.PageTypes.Article'.]
   ASP.templates_public_pages_newsroom_aspx.__DataBind__control4(Object sender, EventArgs e) in c:\projects\Site\Templates\Public\Pages\NewsRoom.aspx:25
   System.Web.UI.Control.OnDataBinding(EventArgs e) +132
The PageType is inheriting from PageData.
 
Any ideas in resolving the issue?
#54811
Nov 03, 2011 16:42
Vote:
 

you, you need to use PageTypeBuilder to do this from my experience

(Article)PageTypeBuilder.PageTypeResolver.Instance.ConvertToTyped((PageData)Container.DataItem)

#54812
Nov 03, 2011 21:45
Vote:
 

Gaaah, works on some but not on others.

 

Thanks Joshua. Error handling...

#54825
Nov 04, 2011 14:22
Vote:
 

Do all the pagetypes inherit from typed page data.  or is container.DataItem null.  that could be an issue since it can't cast to type Pagedata

#54826
Nov 04, 2011 15:04
Vote:
 

Yes, they do inherit from Pagedata, through base classes.

 

The DataItem should not be null. It all worked before the upgrade to R2 

#54827
Nov 04, 2011 15:10
Vote:
 

What is the error that you are recieving now

#54828
Nov 04, 2011 15:11
Vote:
 
<EPiServer:PageList runat="server" ID="ConnectedArticles">
<ItemTemplate>
<div class="post">
<div class="heading">
<h3>
<EPiServer:Property ID="Property3" runat="server" PropertyName="PageLink" />
</h3>
</div>
<p class="timestamp"><%# ReturnPublishDate((EPiServer.Core.PageData)Container.DataItem)%> |
<%# GetCategoryName((EPiServer.Core.PageData)Container.DataItem)%></p>

<div class="mainBody ingress">
<p><%# ReturnIngress((Article)PageTypeBuilder.PageTypeResolver.Instance.ConvertToTyped((EPiServer.Core.PageData)Container.DataItem))%></p>
</div>
</div>
</ItemTemplate>
</EPiServer:PageList>
 
Actually this PageList works - no errors, but it would be better using a method converting to correct page type or using code behind instead of the page(view). 
#54829
Nov 04, 2011 15:18
Vote:
 

Weird, i have used this on all my 6r2 projects and it works.  Is it something maybe that isn't of type article.  I haven't ran into that issue.

I wrote an extension method for passing in data item and returns the type instead of that long chain of code.  data.Get<Article>() returns article page.

#54830
Nov 04, 2011 15:23
Vote:
 

Yepp, got some help from Joel Abrahamson and trying out with the AsTyped extension.

 .AsTyped() when getting a PageCollection solved it.
PageDataCollection pdc = DataFactory.Instance.FindPagesWithCriteria(pageLink, criterias).AsTyped();

Thank you for your help, Joshua,

 

#54832
Edited, Nov 04, 2011 16:01
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.