Chapter 10. List-Bound Controls, Part I

ASP.NET offers three related list-bound controls: Repeater, DataList, and DataGrid. These controls support the display of repeating data such as database reports, shopping carts, menus, and query results. These are among the most powerful controls in ASP.NET and mastering them is key to creating viable commercial applications.

The Repeater is a lightweight control that derives directly from the base Control class. It is lookless, which means that there is no predefined user interface or style; the developer is free to provide virtually any look to the Repeater through the use of templates. Repeaters are ideal when the developer must maintain complete control over the look and feel of the control.

Note

Templates are HTML elements that define the content and rendering of a Repeater or other control. You create a template as you would any HTML element, for example:

<template name="myTemplate">
    Programming C#
</template>

Within the template you may nest other HTML elements such as labels and text. Templates are discussed in detail in Chapter 13.

The DataList control derives from the BaseDataList class, as does the DataGrid. The BaseDataList class derives in turn from the WebControl class, which derives from the Control class (see Figure 4-5 in Chapter 4, which depicts the relationship of controls in the System.Web.UI.WebControls namespace). The DataList displays either a columnar or a normal HTML flow layout. It also provides support for selection, editing, and deleting of items.

Tip

Flow layout describes how the text on an HTML page will be displayed. In normal HTML the text is written across the page, in columnar layout the text is arranged in columns.

The DataGrid displays its data in a table of columns and rows. Like the DataList, the DataGrid provides style and appearance properties as well as selection and editing. The DataGrid also supports sorting of columns and paging through the data. Unlike the DataList, the DataGrid does not support template properties; the rows of the control cannot be controlled by templates. It is possible to add a TemplateColumn object to the DataGrid, however, which allows the use of templates within that column. We’ll return to templates later in this chapter.

Table 10-1 illustrates the principal differences among the three list-bound controls.

Table 10-1. Features of the list-bound controls

Feature

Repeater

DataList

DataGrid

Column layout

No

Yes

No

Flow layout

Yes

Yes

No

Paging

No

No

Yes

Select/ Edit/Delete

No

Yes

Yes

Sort

No

No

Yes

Style properties

No

Yes

Yes

Table layout

No

No

Yes

Templates

Yes

Yes

Columns/ optional

If you examine this table closely, you find that Repeaters have no look to them, and offer only a flow layout. They are entirely controlled by templates. DataLists offer either flow or column layout, and their look is controlled by style properties and templates. DataLists also support selection, editing, and deletion, but not sorting and paging. DataGrids, finally, support table layout only; their look is controlled with style properties and optionally with column templates, and they support not only selection, editing, and deletion, but also sorting and paging.

This chapter will introduce and discuss the DataGrid control using an ArrayList as the data source. In Chapter 11, we’ll explain how to access data in the database. Chapter 12 will cover updating data using ADO.NET. Then in Chapter 13, we’ll return to the DataGrid control to bind to a database and continue the discussion of list-bound controls by examining the DataList and Repeater controls.

Shared Properties and Collections

Before delving into the specifics of each list-bound control, this chapter will examine the features all three controls share in common.

DataSource

All the list-bound controls have a DataSource property. This property defines the source for data binding, as you’ve seen earlier with other controls. A DataSource can be any object that implements the System.Collections.ICollection interface: an array, a dataset, or some other homogeneous collection of objects. Often, the data source will be a System.Data.Dataview object, as discussed in Chapter 11. For simplicity, in this chapter you will continue to use an ArrayList object, though you can use any object that implements System.Collections.ICollection, such as a HashTable or an Array object.

Items

The three list-bound controls also contain an Items property that returns a collection of objects representing an item (row) in the data source collection. You use the Items collection to manipulate the items in the list control programmatically.

Tip

The Repeater control’s Items property returns a collection of RepeaterItem objects, the DataList control’s Items property returns a collection of DataListItem objects, and the DataGrid control’s Items property returns DataGridItem objects.

The DataGridItem object has an ItemType property that returns an enumerated ListItemType. The members of the ListItemType enumeration are shown in Table 10-2.

Table 10-2. Members of the ListItemType enumeration

List item type

Purpose

AlternatingItem

Every other item

EditItem

Used for in-place editing; see Chapter 13

Footer

Display footer for control

Header

Display header information for control

Item

Displays information about data item

Pager

Display paging information

SelectedItem

The item the user has selected

Separator

Appears between items

Manipulation of the various types of members of the Items collection will be demonstrated throughout the rest of this chapter.

All of the list-bound controls follow the explicit binding method. When its DataBind method is called, the list-bound control enumerates its data source, creating DataListItems and initializing them from the DataSource items. The DataListItem objects are then added to the list-bound control’s Items collection. Because this happens only on demand, there are fewer round trips to the server than might otherwise be expected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.11