Unable to cast object of type 'PageImage' to type 'PageItem'

Topics: Developer Forum
Aug 22, 2007 at 2:11 PM
Hi Cristian, you did a great job with this cms. I have to say I'm quite a noob in ASP .NET but this is very understandable, thanks to the big efforts you did on your website to explain and promote N2.

I deployed the example SQLWebSiteCS to enter in the depths of N2, and I tried to create another class called PageImage.cs to offer more choice of templates to the user.

Here is the code :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

N2.Item("Page Image", "PageImage")
public class PageImage : MyItemBase
N2.Details.EditableFreeTextArea("Text", 100)
public virtual string Text
get { return (string)GetDetail("Text"); }
set { SetDetail("Text", value); }

N2.Details.EditableCheckBox("TextBoite", 80)
public virtual string TextBoite
get { return (string)GetDetail("TextBoite"); }
set { SetDetail("TextBoite", value); }

N2.Details.EditableImage("Image", 95)
public virtual string Image
get { return (string)GetDetail("Image"); }
set { SetDetail("Image", value); }


When I saved a new page in the edit mode, using this template, I have the following error :

Unable to cast object of type 'PageImage' to type 'PageItem'

It may seem a bit trivial, but I really don't understand why I have this mistake.

Thanks for all, and great job again,

Aug 22, 2007 at 2:55 PM
Edited Aug 22, 2007 at 3:43 PM
Aug 22, 2007 at 4:00 PM
You need to override the TemplateUrl property and reference an aspx page.

[N2.Item("Page Image", "PageImage")]
public class PageImage : MyItemBase
public override string TemplateUrl
  get { return "~/The/Path/To/A/Page.aspx";}

The reason you are getting that exception is that your class is beeing casted to the class used by another template (probably default.aspx)

This page might explain this: http://n2cms.com/Documentation/Templates.aspx

Aug 27, 2007 at 8:42 AM
Thanks for your response.

I skipped the overriding of TemplateUrl, and now it's ok. Thanks for all.