WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 201 users


Alternating styles in ListView without AlternatingItemTemplate

Blogger : Atlas and more
All posts : All posts by Atlas and more
Category : ASP.NET
Blogged date : 2008 Jul 23

ListView (the server-side control), like all repeating data controls in ASP.NET, has an AlternatingItemTemplate, but it would be a shame to have to copy all the markup in the ItemTemplate into AlternatingItemTemplate, just to alternate styles on the items. It's quite likely that only css classes will change between the two, and redundancy is bad <- big scoop here.

But there is a simpler way. From within any template, you have access to the current index of the row within the whole data set, using Container.DataItemIndex, and within the currently displayed items, using Container.DisplayIndex. This gives us an easy way to alternate styles:

<ItemTemplate>
  <li class="<%# Container.DisplayIndex % 2 == 0 ? "even" : "odd" %>">
<%# Eval("Name") %>
</li> </ItemTemplate>

Just define the even and odd classes in your stylesheet and you're pretty much done.

The full source code for the page is attached below:
http://weblogs.asp.net/bleroy/attachment/6436332.ashx


Read comments or post a reply to : Alternating styles in ListView without AlternatingItemTemplate
Page 399 of 1924
Next | Last

.NET News Categories








Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card