Anders Hattestad
Dec 6, 2010
  7328
(1 votes)

CurrentPage in user controls

If you in CMS 5 had a user control inside a PageList item template the CurrentPage got set to that page. This doesn't seem to work in CMS 6 as far as I can tell.  But if you use EPiServer:Property it will find the active PageData object. It will even work in a repeater as long as the databind is done before preRender (strange….)

 

The trick if you want CurrentPage to return the active PageData object is to implement the same concept from EPiServer:Property inside your user control base class. But since the UserControlBase don’t have many virtual methods you need to start from scratch, and add all stuff :(

I added a generic base class

Code Snippet
  1. public class IteraControlBase<T> : IteraControlBase where T : PageData
  2. {
  3.     public new T CurrentPage
  4.     {
  5.         get
  6.         {
  7.             return base.CurrentPage as T;
  8.         }
  9.     }
  10. }

You can the easy add logic that find the CurrentPage from a repeater also

the fun part is done in:

CurrentPage
  1. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  2. public PageData CurrentPage
  3. {
  4.     get
  5.     {
  6.         if (this._customCurrentPage == null)
  7.         {
  8.             bool foundDataItem = false;
  9.             object dataItem = null;
  10.  
  11.             Control bindingContainer = base.BindingContainer;
  12.             while ((bindingContainer != null) && !this.QualifiesPageData(dataItem))
  13.             {
  14.                 dataItem = DataBinder.GetDataItem(bindingContainer, out foundDataItem);
  15.                 if (dataItem == null && bindingContainer is RepeaterItem)
  16.                 {
  17.                     RepeaterItem repetaerItem = bindingContainer as RepeaterItem;
  18.                     int index = repetaerItem.ItemIndex;
  19.                     Repeater parent = repetaerItem.Parent as Repeater;
  20.                     if (parent != null)
  21.                     {
  22.  
  23.                         if (parent.DataSource is IList)
  24.                         {
  25.                             object o = (parent.DataSource as IList)[index];
  26.                             if (o is PageData)
  27.                             {
  28.                                 dataItem = (o as PageData);
  29.                                 foundDataItem = true;
  30.                             }
  31.  
  32.                         }
  33.                     }
  34.                 }
  35.                 bindingContainer = bindingContainer.BindingContainer;
  36.             }
  37.             if ((foundDataItem && (dataItem != null)) && this.QualifiesPageData(dataItem))
  38.             {
  39.                 if ((this._pageSource == null) && !base.DesignMode)
  40.                 {
  41.                     IPageSource source = bindingContainer as IPageSource;
  42.                     if (source != null)
  43.                     {
  44.                         this._pageSource = source;
  45.                     }
  46.  
  47.                 }
  48.             }
  49.             if (dataItem == null)
  50.             {
  51.  
  52.                 _customCurrentPage = this.PageSource.CurrentPage;
  53.             }
  54.             else
  55.                 _customCurrentPage = this.GetPageData(dataItem);
  56.  
  57.         }
  58.         return this._customCurrentPage;
  59.     }
  60.     set
  61.     {
  62.         this._customCurrentPage = value;
  63.     }
  64. }

 

 

Rest of code is uploaded in the code section

Dec 06, 2010

Comments

Erik Nordin Wahlberg
Erik Nordin Wahlberg Dec 6, 2010 03:48 PM

Hmm. CurrentPage has always been the "current page" and not the page from the repeater-item, hasn't it?

If you want the "item page" there is a one line command to get it.. :)
(this.NamingContainer as IPageSource).CurrentPage

Anders Hattestad
Anders Hattestad Dec 6, 2010 05:42 PM

Have been looking around with refector, and have to agree with you :) I thought and have used I thought that before, but I have to be wrong ...

But your trick will not work if you are inside another usercontroll, or inside a INamingContainer inside the itemtemplate before your user control.

But I think that the correct thing is that the CurrentPage should refere to the page in the repeater/pagelist and not the global page. A lot easier to reuse code that way

Dec 7, 2010 09:10 AM

FYI: I just marked all methods and properties in UserControlBase as virtual for the CMS 6 R2 branch.

Anders Hattestad
Anders Hattestad Dec 7, 2010 03:39 PM

Thanks.... will you are at it could you mark all the methods in LanguageManager virtual also :)

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024