tags:

views:

35

answers:

1

I want to alternate rowcolor in a GridView, using pure CSS, ie: I don't want to use asp.net themes; I'd rather not have to use jQuery, or define define AlternatingRowStyle-BackColor on each gridview (unless there's a reason I must).

Here's my CSS (that isn't working):

.gridView {font-size:11px
}

.gridView tr:nth-child(even) {background-color: #FFF}
.gridView tr:nth-child(odd) {background-color: #FFCC00}

.gridView tr:nth-child(even) td {background-color: #FFF}
.gridView tr:nth-child(odd) td {background-color: #FFCC00}

(I included the .gridView {font-size:11px} just to confirm I am using the proper CssClass.)

Is this not possible, or am I doing something wrong.

+3  A: 

You are using CSS3, which is not supported by all browsers. To do this in a cross-browser compatible way you need to use alternating class, jQuery, or some other method to do this.

The alternating class is a fairly elegant way to do so, IMHO.

UPDATE: This shows the gridview way:

<asp:gridview id="CustomersGridView" runat="server">        
    <RowStyle CssClass="Row" />
    <AlternatingRowStyle CssClass="AltRow" />
</asp:gridview>
Dustin Laine
By "alternating class", do you mean, I have to define a class on each alternate roww within my html?
tbone
You are using the ASP.NET GridView correct? See my answer, I updated it.
Dustin Laine
Did this work for you?
Dustin Laine
Ah, very nice, and quite elegant indeed. A similar but slightly different method can be seen here: http://stackoverflow.com/questions/607547/define-global-site-style-for-aspgridview-with-plain-css-without-using-vs-skins
tbone