Try our conversational search powered by Generative AI!

Anders Hattestad
Dec 6, 2010
  7291
(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
Anonymous Tracking Across Devices with Optimizely ODP

An article by Lead Integration Developer, Daniel Copping In this article, I’ll describe how you can use the Optimizely Data Platform (ODP) to...

Daniel Copping | Apr 30, 2024 | Syndicated blog

Optimizely Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024

Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024