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
Comments