I’m probably the last person on earth to discover this, but it is definitely cool….
While the Gridview (and DataGrid) are powerful tools for displaying data, they carry a bit more heft than is needed frequently. Often you can acheive superior results with a Listview (or Repeater) with more fine-grained control and less overhead.
In this case I was using a Repeater inherited from previous versions and have not yet attempted a conversion to Listview (and may not ever). The Repeater control offers an alternate row template which could be used for a lot of things. But mostly it’s probably used, as we had in this case, to put in alternating row backgrounds. The problem is that you create an essentially duplicate row code just to change the styling.
Being at that stage in jQuery where if you’ve just bought a new hammer, every problem looks like a nail… It occurred to me that I had just seen a jQuery script for this pattern:
$("#myTable tr:even").addClass("alternaterowstyle");
You can even:
$("myTable tr:odd").addClass("rowstyle");
$("#myTable tr:even").addClass("alternaterowstyle");
So, in this case, the Repeater is rendering a table. All that’s needed is to apply the jQuery on document ready.
Listview should work essentially the same – haven’t tried it yet.
By having a single item template, you don’t wind up with accidental differences between rows. Or worse, unnoticed differences – because they never stay unnoticed, do they?
If you are running your control inside an UpdatePanel, it’s a bit trickier. But not much – next post.