SaaS CMS has officially launched! Learn more now.

PageTypeBuilder page type casting returns null


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)

Jun 21, 2011 6:00

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.

Jun 21, 2011 7:32

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.

Jun 21, 2011 9:35

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

Jun 21, 2011 9:38

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.

Jun 21, 2011 10:03

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.
Dec 06, 2011 5:09

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,

public class ShoppingItemListType : ShoppingPageType




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

Edited, Dec 06, 2011 5:12

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.

Dec 06, 2011 9:01

You are a LEGEND! Thanks Joel!

Dec 07, 2011 23:42

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

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.
* 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.