2

How can I get the Page Type for the current page. I tried

CurrentPage.GetType();

but no success. I need to check if the Page Type equals a specific type in order to do something or not.

Thanks

tif
  • 1,109
  • 2
  • 12
  • 32

4 Answers4

8

You need to check the PageTypeName or PageTypeID properties, like this:

if(CurrentPage.PageTypeName == "StartPage")
    // Do something

Check the reference: http://sdk.episerver.com/library/cms5/html/AllMembers_T_EPiServer_Core_PageData.htm

The GetType() method is declared on System.Object and returns the System.Type for the object. Is is available on all types in the .NET Framework since all types inherit from System.Object.

Mikael Östberg
  • 16,982
  • 6
  • 61
  • 79
  • 1
    I have a similar problem. I have a site with both EPiServer Pages and Non-EpiServer Pages, the problem is that when checking the type on a Non-EpiServer Page "CurrentPage" returns values as if I was in the start page. I need to check if the page is an EpiServer page or not. Thanks – tif Apr 28 '11 at 13:00
  • 2
    Your EPiServer pages probably inherit from a certain base class while the other pages dont, right? Just check `if(this is MyEpiPageBaseClass)` in the page and you should be fine. – Mikael Östberg Apr 29 '11 at 07:17
5

With a page type builder strongly typed class you can use the C# is keyword

e.g

if (CurrentPage is SomeStronglyTypeClass) 
tompipe
  • 949
  • 6
  • 8
3

CurrentPage.PageTypeName gives you the name of the PageType

Mari
  • 61
  • 2
2

If you're using Page Type Builder you can also use the PageTypeResolver class.

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72