views:

559

answers:

7

What would you realistically use OnItemDataBound for on a Repeater ?

+1  A: 

I've used it when I needed to massage the data a bit for each item in the repeater.

Kevin Tighe
Is the repeater the right place to do that though? Should be doing logic like that in a user/server control IMO
pzycoman
I guess it depends on the scenario. When I used it I was just doing some formatting work with the data before it was displayed. It's certainly not appropriate to put any business logic there.
Kevin Tighe
+1  A: 

One use could be dynamic control generation based on information that is only available from the bound data item at the time it is bound the Repeater.

Jay S
+4  A: 

"This event provides you with the last opportunity to access the data item before it is displayed on the client. After this event is raised, the data item is nulled out and no longer available."

~http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.onitemdatabound.aspx

Chris Ballance
+1  A: 

It's necessary if you want to customize an individual item within the template with dynamic controls or data that wasn't a part of the resultset you originally bound to the repeater.

BC
A: 

I've used it to implement a nested repeater. In the outer repeater's ItemDataBound event handler, you run the code to databind the current item's instance of the inner repeater.

Mike Powell
Which you could also do with DataSource="<%# DataBinder.Eval(Container.DataItem, "Source" %>" :). Sorry not trying to argue, just trying to find a proper use for it.
pzycoman
A: 

I find data-binding syntax difficult to read and debug. I don't think I've ever done anything with OnItemDataBound that I couldn't have done by putting a data-binding expression into the markup, but if I want to change it later, I personally find it a lot easier to just set a break point in OnItemDataBound and then use the immediate window to nail down what I want to have rendered. I'm also an adherent to the apparently fading (e.g. Ruby) dictum that code and markup should be separate.

A: 

Think of it like this. As your item is being created, all the OnDataBinding events are firing for that specific item. There could be many things that build up one item so many OnDataBinding events could be called. Once it is all done, this is the event that is triggered so you can apply some final 'touches' and all the OnDataBinding events are done at this time.

Eg. You item fills 10 fields of data and does all sorts of stuff when bound. Once all that data is filled up you want to check certain pieces of that data that has now been created and set something to the whole item like the entire row color or some icons based on data from the whole item.

Kelsey