Views: 6571
Number of votes: 1
Average rating:

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

Erik Nordin Wahlberg
( By Erik Nordin Wahlberg, 12/6/2010 3:48:53 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
( By Anders Hattestad, 12/6/2010 5:42:04 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

( 12/7/2010 9:10:38 AM)

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

Anders Hattestad
( By Anders Hattestad, 12/7/2010 3:39:16 PM)

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

Please login to comment.