Question

What would you realistically use OnItemDataBound for on a Repeater ?

Was it helpful?

Solution

"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

OTHER TIPS

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.

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

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top