Reputation: 23551
The ASP.NET ListView control like most databound ASP.NET controls has an AlternatingItemTemplate which you can use for example to render different css class for odd and even rows. The control also has GroupTemplate which you can use to group items and display for example 3 items in TDs that are grouped in a TR. However there is no alternating group template. My question is how to alternate the class attribute of a tr in a GroupTemplate. Basically I want to have 3 items in a TR and alternating styles on odd and even TRs.
I want to avoid nesting one databound control in another and I've already tried binding the class of the TR in a databinding expression <%# Alternate() %>
but it seems that databinding expressions are only called in the itemtemplate (obviously they are used in the databind event and it is not called for the group template)
Any ideas?
Example code:
<asp:ListView ID="lv1" runat="server" EnableViewState="false" GroupItemCount="3">
<LayoutTemplate>
<table class="asd">
<asp:PlaceHolder ID="groupPlaceholder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr class='<%# GetAlternatingCssClass() %>'>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("Url") %>'> <%# Eval("Text") %> </asp:HyperLink>
</td>
</ItemTemplate>
</asp:ListView>
Upvotes: 1
Views: 3188
Reputation: 21
http://csharpin.blogspot.com/2010/06/listview-alternating-group-template.html
Upvotes: 2
Reputation: 43074
Firstly:
<GroupTemplate>
<tr class='<%# GetAlternatingCssClass() %>'>
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</tr>
</GroupTemplate>
will only get evaluated once for the ListView as it's evaluated at the point the page is processed, not for every time the element is accessed. To implement this as intended here I think you'd need to be overriding the Render method for the ListView to alternate the class based on the GroupItemCount. I've not coded this so this is supposition.
The GroupTemplate is intended to deliver a kind of flow layout where each item is a self contained element, i.e. a complete product listing, and where the items and rows are not connected (as the would be as cells in a row representing fields in a record). As such alternating the row colour does nothing to help data representation but is purely asthetic (and possibly confusing for users used to seeing different records identified in this way, it may cause them to make inferences about the data relationships that don't actually exist).
I think you are going to have to do a lot of work to get what you want from the basic ListView, I think the first question to ask yourself is why you want this and is it a user requirement or your requirement based on asthetics (nothing wrong with that per se but you appreciate that the controls are designed for function rather than asthetic). If it isn't a user requirement, do they want to pay for the effort to resolve this?
Upvotes: 1