by aanund
26. May 2011 01:36
A PageList where it is possible to define different Templates for different pagetypes. More of a proof-of-concept than anything else.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EPiServer.Web.WebControls;
using System.Web.UI;
using EPiServer.Core;
using System.Collections.Specialized;
using System.Collections;
using System.ComponentModel.Design;
using System.ComponentModel;
namespace Custom.Web.Code
{
[ParseChildren(true)]
public class PageTypePageList : PageList
{
private PageTypeTemplateCollection _pageTypeTemplates;
[PersistenceMode(PersistenceMode.InnerProperty)]
public List<PageTypeTemplateContainer> PageTemplates
{
get
{
if (_pageTypeTemplates == null)
{
_pageTypeTemplates = new PageTypeTemplateCollection();
}
return _pageTypeTemplates;
}
}
protected override void CreateChildControls()
{
if (ItemTemplate != null)
{
PageDataCollection pages = base.GetPages();
if (pages.Count != 0)
{
PageData page = null;
if (!PageReference.IsNullOrEmpty(this.PageLink))
{
page = this.GetPage(this.PageLink);
}
if (HeaderTemplate != null)
{
Control container = new PageTemplateContainer(page);
HeaderTemplate.InstantiateIn(container);
this.Controls.Add(container);
}
this.PreparePagingControls(pages);
for (int i = 0; i < pages.Count; i++)
{
Control control2 = new PageTemplateContainer(pages[i]);
ITemplate template = GetTemplateForPageData(pages[i]);
template.InstantiateIn(control2);
this.Controls.Add(control2);
}
if (FooterTemplate != null)
{
Control control3 = new PageTemplateContainer(page);
FooterTemplate.InstantiateIn(control3);
this.Controls.Add(control3);
}
this.CreatePagingControls(pages);
}
}
}
protected ITemplate GetTemplateForPageData(PageData pd)
{
var template = PageTemplates.SingleOrDefault(t => t.PageTypeId == pd.PageTypeID);
if (template == null)
{
template = PageTemplates.SingleOrDefault(t => StringComparer.OrdinalIgnoreCase.Compare(t.PageTypeName, pd.PageTypeName) == 0);
if (template == null)
{
return ItemTemplate;
}
}
return template.ItemTemplate;
}
}
[DefaultProperty("ItemTemplate"), ParseChildren(true)]
public class PageTypeTemplateContainer
{
public int PageTypeId { get; set; }
public string PageTypeName { get; set; }
[TemplateContainer(typeof(PageTemplateContainer)), PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ItemTemplate { get; set; }
}
public class PageTypeTemplateCollection : List<PageTypeTemplateContainer>
{
}
}
With will be used as follows in the aspx:
<Custom:PageTypePageList runat="server">
<HeaderTemplate>
<div class="searchResults">
</HeaderTemplate>
<ItemTemplate>
<div class="item">
<h2><EPiServer:Property runat="server" PropertyName="PageLink" /></h2>
<p><EPiServer:Property runat="server" PropertyName="MainIntro" /></p>
</div>
</ItemTemplate>
<PageTemplates>
<Custom:PageTypeTemplateContainer PageTypeName="[Public]Article">
<ItemTemplate>
<div class="item" style="border: solid 1px red;">
<h2><EPiServer:Property runat="server" PropertyName="PageLink" /></h2>
<p><EPiServer:Property runat="server" PropertyName="MainIntro" /></p>
<p>Publisert: <%# ((EPiServer.Core.PageData)Container.DataItem).Created.ToString("dd.MM.yyyy") %></p>
</div>
</ItemTemplate>
</Custom:PageTypeTemplateContainer>
<Custom:PageTypeTemplateContainer PageTypeId="11">
<ItemTemplate>
<div class="item" style="border: solid 1px blue;">
<h2><EPiServer:Property runat="server" PropertyName="PageLink" /></h2>
<p><EPiServer:Property runat="server" PropertyName="MainIntro" /></p>
<p>Publisert: <%# ((EPiServer.Core.PageData)Container.DataItem).Created.ToString("dd.MM.yyyy") %></p>
</div>
</ItemTemplate>
</Custom:PageTypeTemplateContainer>
</PageTemplates>
<FooterTemplate>
</div>
</FooterTemplate>
</Custom:PageTypePageList>
It is admittedly a bit heavy on the control nesting.