Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
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.
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.
The object you are trying to cast, where do you get it from? Can you post some more of the code?
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.
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.
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
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.
Thank god I found this post, never could have figured that one out on my own!
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)