PageTypeBuilder page type casting returns null

Vote:
 

I have a pagetype called PageTypeAPage (PageTypeAPage.cs) and I have a page template called PageTypeA (PageTypeA.aspx) which inherits from TemplatePage<PageTypeAPage>.

Everything's good, the page type gets registered and I'm able to create a new page with that type. I'm able to get a PageData reference of that new page also. So far so good.

Howeven when I cast that page as PageTypeAPage, I always get null and I have no idea why.

PageTypeAPage page = typeAPage as PageTypeAPage (returns null)

#51682
Jun 21, 2011 6:00
Vote:
 

In what context are you casting the page? If you want to use it on the TemplatePage<PageTypeAPage> template, the CurrentPage property will already be of type PageTypeAPage.

#51684
Jun 21, 2011 7:32
Vote:
 

Thanks for the reply Magnus.

 

I'm casting it from the code behind of another ASPX file.

I've seen some code do the casting successfully.

 

Initially I just want to access a property like page.MyProperty instead of page.Property["MyProperty"].

I just want to make it work first.

#51696
Jun 21, 2011 9:35
Vote:
 

The object you are trying to cast, where do you get it from? Can you post some more of the code?

#51697
Jun 21, 2011 9:38
Vote:
 

typeAPage as PageTypeAPage

will return null if the typeAPage isn't of the type PageTypeAPage. If you debug your code and add a breakpoint at this line. You should be able to check what pagetype typeAPage is of.  As Magnus says it would be good with some more code. Where do you set the variable typeAPage? Perhaps your getting another page then the one your expecting.

#51699
Jun 21, 2011 10:03
Vote:
 

I encountered this now again. Sometimes this happens, sometimes it doesn't and I have no idea why.

Here's a sample code: 

PageData categoryPage = FindCategoryPage(categoryTree.ID, storeRootPage, lang);

ShoppingItemListType editableCategoryPage;

if (categoryPage != null)
{
editableCategoryPage = categoryPage.CreateWritableClone() as ShoppingItemListType;
}
 
The categoryPage retrieved here is of type ShoppingItemListType. This is certain.
public partial class ItemList : ShoppingTemplatePage<ShoppingItemListType>
{
}
I can verity it from edit mode
 
However, the call:
editableCategoryPage = categoryPage.CreateWritableClone() as ShoppingItemListType;
would result to null
 
I had this experience with EPiServer before and then all of a sudden it will just be fixed for no definite reason, even without doing anything.
So now, I'm experiencing it again and I don't know how to fix it because everything looks ok.
 
Hoping someone could shed some light. Thanks.
#55500
Dec 06, 2011 5:09
Vote:
 

PageTypeName would be: [Store] ShoppingItemList

Which is basically derived from this:

[PageType(Filename = "~/Templates/Shopping/Pages/ItemList.aspx",
AvailableInEditMode = true,
DefaultVisibleInMenu = true,
Name = "[Store] ShoppingItemList",
SortOrder = 1020,
AvailablePageTypes=
new[]
{
typeof(ShoppingItemType),
typeof(ShoppingItemListType)
})]

public class ShoppingItemListType : ShoppingPageType
{
...
}

 

 

-------------------

public abstract class ShoppingTemplatePage<T> : TemplatePage where T : TypedPageData

#55501
Edited, Dec 06, 2011 5:12
Vote:
 

Due to a bug in CMS 6 R2 pages that are fetched using the GetPages method, which is also used behind the scenes by FindPagesWithCriteria, aren't returned with a correct type. That is, GetPage and GetChildren will return pages that you can cast, GetPages and FindPagesWithCriteria will not. PTB 1.3.1 and upwards adds an extension method for PageDataCollection named AsTyped that will go through the pages and replace them with correctly typed versions.

So, in FindCategoryPage, if you're using FPWC or GetPages, call AsTyped() on the result.

#55505
Dec 06, 2011 9:01
Vote:
 

You are a LEGEND! Thanks Joel!

#55574
Dec 07, 2011 23:42
Vote:
 

Thank god I found this post, never could have figured that one out on my own!

#57783
Mar 27, 2012 15:56
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.