ListView Alternating Group Template

The ListView have no such thing as alternating the group templates and still is so much needed.


public class aiListView : ListView
{
int groupDisplayIndex = 1; /*update on June 16, 2010*/
private ITemplate _alternatingGroupTemplate;
public virtual ITemplate AlternatingGroupTemplate
{
get { return _alternatingGroupTemplate; }
set { _alternatingGroupTemplate = value; }
}

protected override void InstantiateGroupTemplate(System.Web.UI.Control container)
{

ITemplate template = this._alternatingGroupTemplate;
if (_alternatingGroupTemplate != null && GroupItemCount > 0 && groupDisplayIndex % 2 == 0 /*update on June 16, 2010*/)
{
template = this._alternatingGroupTemplate;
}
else
{
template = this.GroupTemplate;
}

template.InstantiateIn(container);
groupDisplayIndex++;
}
}


The usage:


<ai:aiListView ID="lvProducts" runat="server" GroupItemCount="3">
<LayoutTemplate>
<asp:PlaceHolder ID="groupPlaceholder" runat="server">
</asp:PlaceHolder>
</LayoutTemplate>
<GroupTemplate>
<div class="row">
<asp:PlaceHolder ID="itemPlaceholder" runat="server">
</asp:PlaceHolder>
</div>
</GroupTemplate>
<AlternatingGroupTemplate>
<div class="row alternate">
<asp:PlaceHolder ID="itemPlaceholder" runat="server">
</asp:PlaceHolder>
</div>
</AlternatingGroupTemplate>
<ItemTemplate>
<div>
</div>
</ItemTemplate>
</ai:aiListView>



It produces:


<div class="row">
<div></div>
<div></div>
<div></div>
</div>

<div class="row alternate">
<div></div>
<div></div>
<div></div>
</div>
<div class="row">
<div></div>
<div></div>
<div></div>
</div>

<div class="row alternate">
<div></div>
<div></div>
<div></div>
</div>

..

Comments

Popular posts from this blog

IIS 7.5, HTTPS Bindings and ERR_CONNECTION_RESET

Table Per Hierarchy Inheritance with Column Discriminator and Associations used in Derived Entity Types

About the "The Controls collection cannot be modified because the control contains code blocks" symptom