May
20
by Mike Fleming at 7:44 am (1 Comment) .NET | C-Sharp
This is just a quick little tip to help you display alternating row colors in your C# code for a DataList or GridView control. Using the AlternatingItemTemplate method is a pain, as it forces you to duplicate your entire ItemTemplate block just to add the alternating CSS class. While trying to find another means of accomplishing this I found the Container.ItemIndex syntax. Using this you can write a little if statement with a MOD function in it to have the code display your alternating style. So my DataList now looks like this:
| Title | ID | Edit | Delete |
|---|---|---|---|
| "> | "> | "> |
So every other row will now use a class named trDark. To use this with a GridView you would use the syntax of Container.DisplayIndex.
His_wife14
October 22, 20091:41 pm