Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Comparing List and Iterative Controls


Comparing List and Iterative Controls


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].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.