2.3. Enhancing the Output of a Tabular Display

Problem

You need to display data from a database in a way that lets you organize and enhance the output beyond the confines of the DataGrid or GridView control’s default tabular display. Selecting and editing the data are unimportant as is navigating through the data.

Solution

Use a Repeater control with templates and then bind the data to the control.

In the .aspx file, add a Repeater control and the associated templates for displaying the data.

In the code-behind class for the page, use the .NET language of your choice to:

  1. Create a SqlDataSource.

  2. Set the ConnectionString, DataSourceMode, ProviderName, and SelectCommand properties of the SqlDataSource.

  3. Assign the data source to the Repeater control and bind it.

Figure 2-2 shows the appearance of a typical Repeater in a browser. Examples 2-4, 2-5 through 2-6 show the .aspx and code-behind files for an application that produces this result.

Using templates with Repeater control display output

Figure 2-2. Using templates with Repeater control display output

Discussion

When your primary aim is to organize and enhance the output beyond the confines of the DataGrid and GridView control’s default tabular display, the Repeater control is a good choice because, unlike a DataGrid or GridView, it has associated templates that allow you to use almost any HTML to format the displayed data. It has the advantage of being relatively lightweight and easy to use. When ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.