JavaScript Editor Javascript validator     Javascripts

Main Page


Previous Section Next Section

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.

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. Chapter 13 discusses templates in detail.

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.

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. They support not only selection, editing, and deletion, but also sorting and paging.

This chapter introduces and discusses 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 discuss 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.

    Previous Section Next Section


    JavaScript Editor Javascript validator     Javascripts 
    Bitcoin Dice Site



    ©