# Using IDisposable to stripe a table

What are your thoughts on using an IDisposable to stripe an HTML table like so? It's a blatant abuse of IDisposable but this allows you to use the counter in a using(){} block which gives you a clear block in the markup.

public class StripeCounter : IDisposable
{
private string cssClass = "stripe";

private int counter = 0;

public StripeCounter(){}

public StripeCounter(string cssClass)
{
this.cssClass = cssClass;
}

public MvcHtmlString Stripe()
{
return MvcHtmlString.Create(counter++ % 2 == 1 ? cssClass : string.Empty);
}

public void Dispose() { }
}


And then the markup

@using (var counter = new StripeCounter())
{
foreach (var item in Model)
{
<tr class="@counter.Stripe()">
<td>
...
</td>
...
</tr>
}
}


Regarding comments on using JavaScript to stripe the table:

JavaScript is for behaviour and the striped rows of a table aren't behaviour; they are style, so while it is possible to do so on the client, it seem to me to be something that should be done on the server. For example, if someone has scripts disabled, they would still see the CSS striping, but not JavaScript striping.

• Why are you striping in custom code instead of using jquery (which comes with MVC3)? It is server side work that you can push down to the client. Aug 30 '11 at 17:52
• This falls into the "Just because you can do something doesn't mean you should" category. Art's comment above is the best response to this. A great option is to do this client side via jQuery or even in plain ole' Javascript. Aug 30 '11 at 18:30

• Note, though, that :nth-child (the CSS pseudo-class that lets you do striping with CSS) doesn't work in IE <9 -- or any other browser that doesn't know CSS3.