November Happy Hour will be moved to Thursday December 5th.

Anders Hattestad
Dec 6, 2010
  7383
(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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog

I'm running Optimizely CMS on .NET 9!

It works 🎉

Tomas Hensrud Gulla | Nov 12, 2024 | Syndicated blog

Recraft's image generation with AI-Assistant for Optimizely

Recraft V3 model is outperforming all other models in the image generation space and we are happy to share: Recraft's new model is now available fo...

Luc Gosso (MVP) | Nov 8, 2024 | Syndicated blog