Editor's Note: The ASP.NET2theMax Newsletter has moved from Windevnet to Dr. Dobb's Journal. The newsletter archives are now available at http://www.ddj.com/columns/asp/.
List controls are data-bound controls that display a list of items. The list of items is prepared by looking at the data bound to the control and loading the contained information into ad hoc structures. The control internally maintains a collection of these structures and generates its HTML markup iterating through the items.
In ASP.NET you find several list controls-RadioButtonList, CheckBoxList, Repeater, DataList, DataGrid. List controls belong to two different families: classic list controls and iterative controls.
The first group includes controls such as RadioButtonList, CheckBoxList, ListBox, and DropDownList. These controls don't let users modify the display interface. In other words, users can't intervene in the process that creates the HTML representation of a radio button, a checkbox, or a drop-down item. The type that represents these items is a generic and not particularly feature-rich class-the ListItem class. All of these controls have a property named Items that is a collection of ListItem objects. No events in the lifecycle of these classic list controls ever let users to hook up the creation of the various items.
Iterative controls are the second family of list controls available in ASP.NET. To this family belong very popular controls such as the DataGrid, Repeater, and DataList control. All these controls support templates and a significantly richer programming model. As a result, programmers can hook up the internal rendering events of the controls and modify the appearance and layout of the various items. For example, if you have a grid's column that represents a cost, you can render it in red if it is greater than a certain threshold and in black in all other cases. This sort of flexibility is simply impossible to reach with classic list controls.
Just like classic list controls, iterative controls work by applying templates repeatedly to all bound data items. The big difference is all in the fact that iterative controls expose a few events for developers to hook up and customize the rendering process. There are mainly two key events: ItemCreated and ItemDataBound.
The ItemCreated event is fired right after a constituent item of the control is created. A constituent item can be the header, the footer, the pager, a data item, or perhaps an alternating item. (An alternating item is a data item displayed on even rows like the second, the fourth, and so on.) Each control has its own set of constituent items, but all controls have at least a header, a footer, and a bunch of data items. The ItemCreated event handler receives a control-specific item object-RepeaterItem, DataListItem, or DataGridItem. An item object contains information like the 0-based index of the item, the corresponding HTML row, and the data item bound to the row.
The ItemDataBound event follows ItemCreated and is fired when the data item object has been bound to the item object. For example, if the DataGrid control is bound to an array of strings, the data object for individual items is the string. When the i.th string is bound to the i.th item, the ItemDataBound event is fired to the hosting environment.
Unlike ItemCreated, the ItemDataBound event is not fired for data-bound items like header, footer, and pager.
You need to handle one or both events to customize the final appearance of the iterative control and make it look context sensitive. If you need to change the layout of the item, you wire to ItemCreated; if you need to process the data being displayed, you have to wait for ItemDataBound.
Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant
based in Rome, Italy. Dino is a contributing editor to Windows Developer
Network and MSDN Magazine, and the author of several books for Microsoft
Press including Building Web Solutions with ASP.NET and ADO.NET
and Applied XML Programming for .NET. Contact Dino at [email protected].