Recipe 2.2. Selecting the Right Tabular Control


Problem

You want to use an ASP.NET control to display some data in a tabular format.

Solution

Use a Repeater, DataList, DataGrid, or GridView control. Always choose the smallest and fastest control that meets your needs, which invariably will be influenced by other criteria as in these examples:


If you need a quick and easy solution

Use a GridView.


If you need a lightweight read-only tabular display

Use a Repeater.


If you need your solution to be small and fast

Use a Repeater (lightest) or DataList (lighter).


If you want to use a template to customize the appearance of the display

Choose a Repeater or DataList.


If you want to select rows or edit the contents of a data table

Choose a DataList, a DataGrid, or a GridView.


If you want built-in support to sort your data by column or paginate its display

Choose a GridView.


If you want to use custom pagination

Choose a DataGrid.

Discussion

ASP.NET provides four excellent options for displaying tabular data: Repeater, DataList, DataGrid, and GridView. Each comes with trade-offs. For instance, the GridView control is versatile, but you can pay a price in terms of performance. On the flip side, the Repeater control is lighter weight but is for read-only display; if you later decide you need to edit your data, you will have to rework your code to use the DataList, DataGrid, or GridView control instead (unless, of course, you want to embark on your own custom coding odyssey).

The impact on performance is because ASP.NET creates a control for every element of a DataGrid and GridView control, even white space, which is built as a Literal control. Each of these controls is responsible for rendering the appropriate HTML output. The DataGrid and the GridView are, therefore, the heavyweights of the grid control group because of the server processing required to build the applicable output. The DataList is lighter and the Repeater lighter still.

Table 2-1 summarizes the built-in features supported by the tabular controls and only includes controls that support data binding. (A standard Table control is not included because it does not inherently support data binding though individual controls placed in a table can be data bound.) With custom code, there are almost no limits to what you can do to modify the behavior of these controls.

Table 2-1. Comparative summary of native tabular control features

Feature

Repeater control

DataList control

DataGrid control

GridView control

Default appearance

None (template-driven)

Table

Table

Table

Automatically generates columns from the data source

No

No

Yes

Yes

Header can be customized

Yes

Yes

Yes

Yes

Data row can be customized

Yes

Yes

Yes

Yes

Supports alternating row customization

Yes

Yes

Yes

Yes

Supports customizable row separator

Yes

Yes

No

Yes

Footer can be customized

Yes

Yes

Yes

Yes

Supports pagination

No

No

Yes

Yes

Supports custom paging

No

No

Yes

No

Supports sorting

No

No

Yes

Yes

Supports editing contents

No

Yes

Yes

Yes

Supports selecting a single row

No

Yes

Yes

Yes

Supports selecting multiple rows

No

No

No

No

Supports arranging data items horizontally or vertically (from left-to-right or top-to-bottom)

No

Yes

No

No

Supports sorting and paging using asynchronous callbacks (see Recipe 2.15)

No

No

No

Yes


Performance issues aside, you must consider other aspects when choosing a tabular control. As a rule, the DataGrid and GridView work well for a quick-and-dirty tabular display (see Recipe 2.2) and for other situations in which you think you'll be reasonably satisfied with its default appearance and behavior. Indeed, because the DataGrid and GridView are so versatile, this chapter provides many recipes for modifying and adapting them. However, if you anticipate needing a lot of flexibility in controlling the organization and layout of the tabular display or you do not need to edit or paginate the data, you may want to consider using the DataList or Repeater instead. For example, Recipe 2.3 shows how you can use templates to organize and enhance the output of a tabular display. Take a look at that recipe's output (Figure 2-2) to see what we're driving at. Some upfront planning in this respect can save you considerable time and effort down the road.



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net