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
- public class IteraControlBase<T> : IteraControlBase where T : PageData
- {
- public new T CurrentPage
- {
- get
- {
- return base.CurrentPage as T;
- }
- }
- }
You can the easy add logic that find the CurrentPage from a repeater also
the fun part is done in:
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public PageData CurrentPage
- {
- get
- {
- if (this._customCurrentPage == null)
- {
- bool foundDataItem = false;
- object dataItem = null;
- Control bindingContainer = base.BindingContainer;
- while ((bindingContainer != null) && !this.QualifiesPageData(dataItem))
- {
- dataItem = DataBinder.GetDataItem(bindingContainer, out foundDataItem);
- if (dataItem == null && bindingContainer is RepeaterItem)
- {
- RepeaterItem repetaerItem = bindingContainer as RepeaterItem;
- int index = repetaerItem.ItemIndex;
- Repeater parent = repetaerItem.Parent as Repeater;
- if (parent != null)
- {
- if (parent.DataSource is IList)
- {
- object o = (parent.DataSource as IList)[index];
- if (o is PageData)
- {
- dataItem = (o as PageData);
- foundDataItem = true;
- }
- }
- }
- }
- bindingContainer = bindingContainer.BindingContainer;
- }
- if ((foundDataItem && (dataItem != null)) && this.QualifiesPageData(dataItem))
- {
- if ((this._pageSource == null) && !base.DesignMode)
- {
- IPageSource source = bindingContainer as IPageSource;
- if (source != null)
- {
- this._pageSource = source;
- }
- }
- }
- if (dataItem == null)
- {
- _customCurrentPage = this.PageSource.CurrentPage;
- }
- else
- _customCurrentPage = this.GetPageData(dataItem);
- }
- return this._customCurrentPage;
- }
- set
- {
- this._customCurrentPage = value;
- }
- }
Rest of code is uploaded in the code section
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
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
FYI: I just marked all methods and properties in UserControlBase as virtual for the CMS 6 R2 branch.
Thanks.... will you are at it could you mark all the methods in LanguageManager virtual also :)