Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Anders Hattestad
Dec 6, 2010
  7436
(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
Optimizely CMS Developer Tools for macOS

Running Optimizely CMS on macOS presents unique challenges, as the platform was traditionally primarily designed for Windows environments. However,...

Tomek Juranek | Mar 15, 2025

Removing a Segment from the URL in Optimizely CMS 12 using Partial Routing

Problem Statement In Optimizely CMS 12, dynamically generated pages inherit URL segments from their container pages. However, in certain cases, som...

Adnan Zameer | Mar 14, 2025 |

Optimizely Configured Commerce and Spire CMS - Figuring out Handlers

I recently entered the world of Optimizely Configured Commerce and Spire CMS. Intriguing, interesting and challenging at the same time, especially...

Ritu Madan | Mar 12, 2025

Another console app for calling the Optimizely CMS REST API

Introducing a Spectre.Console.Cli app for exploring an Optimizely SaaS CMS instance and to source code control definitions.

Johan Kronberg | Mar 11, 2025 |