I like having different styles in my repeater controls for the itemtemplate and alternatingitemtemplate. It makes the data easier to read and is simply cooler. I like using a repeater because it's not as heavy as a datagrid. However, using a repeater is a bit of a pain for something as simple as this.
The benefit of a repeater is you don't have to manage columns like you do with a datagrid. It's just free-form HTML that gets dumped into the page as the repeater is databound to some data. It gives me a lovely spaghetti-like feeling from my old ASP days (just imagining those beautiful days of response.write loops gives me a warm fuzzy feeling'.aaahhh). Now we all know that spaghetti makes a mess and this is no exception. In order to change the style for your alternatingitemtemplate, you need to duplicate all of your HTML code from the itemtemplate and then make a simple little change to your row colour.
This isn't too bad except for the fact that anytime you touch the HTML code, VS.Net (in all of it's helpful glory) warns you that you've got elements with duplicate id's. Of course, that's because you've been forced to copy all of your databound elements into both the itemtemplate and the alternatingitemtemplate. To really help you out, VS.Net goes ahead and just renames those 'offending' elements so that you don't run into collisions. Of course, that's the precise opposite of what I want it to do in this situation, but I can't seem to convince it that my intentions are sound.
This gets even more frustrating when I talk to somebody about my problems. They invariably suggest 'why don't you use a datagrid' You can just define the styles as part of the itemtemplate so that it's separate from your content. It's much cleaner.' Thanks for the help, dude. But I want to use a nice light-weight repeater, damn it! And I also want to implement some cool grouping features that I saw recently.
I've decided to take a different tact. Well, actually, I decided this many moons ago, but could never come up with the proper method for implementing it.
For quite a while, I've been trying to get my repeaters to dynamically load their itemtemplate and alternatingitemtemplate content from template files that I store within my project. I like this idea - the separation of the content without all of the clunkiness of working with the IDE. On top of that, the HTML view of my page is nice and light.
Easier said than done. After following the steps in Bipin Joshi's article on dotnetbips.com, it wouldn't work. And it didn't give me a nice error. As always, the fault was mine. I happened to miss his little line that looks something like this:
<%# Databinder.Eval(((DataGridItem)Container).DataItem,"lastname")%>
Noticing the casting to a DataGridItem would saved my bacon. But I missed it. Recently, Dino Esposito has outlined the reasons that this cast needs to occur:
You shouldn't notice anything different in the page's markup, but this time the item template is being generated from an external compiled resource. A little change is required to make the item template of Figure 1 work unchanged as a distinct ASCX component. The object of the change is the DataBinder expression: <</bloghelper>