Striped table rows in ASP.NET MVC (without using jQuery or equivalent)

Posted by Richard Ev on Stack Overflow See other posts from Stack Overflow or by Richard Ev
Published on 2009-04-16T14:13:13Z Indexed on 2010/06/11 9:42 UTC
Read the original article Hit count: 175

Filed under:
|
|

When using an ASP.NET WebForms ListView control to display data in an HTML table I use the following technique in to "stripe" the table rows:

<ItemTemplate>
    <tr class="<%# Container.DisplayIndex % 2 == 0 ? "" : "alternate" %>">
        <!-- table cells in here -->
    </tr>
</ItemTemplate>

With the following CSS:

tr.alternate
{
    background-color: #EFF5FB;
}

I have just gone through the ASP.NET MVC Movie Database Application tutorial and learnt that in MVC-land table rows can be (must be?) constructed as follows:

<% foreach (var item in Model) { %>
    <tr>
        <td>
            <%= Html.Encode(item.Title) %>
        </td>
        <!-- and so on for the rest of the table cells... -->
    </tr>
<% } %>

What can I add to this code to stripe the rows of my table?

Note: I know that this can be done using jQuery, I want to know if it can be done another way.

Edit

If jQuery (or equivalent) is in your opinion the best or most appropriate post, I'd be interested in knowing why.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about asp.net-mvc