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:
Create a
SqlDataSource
.Set the
ConnectionString, DataSourceMode, ProviderName
, andSelectCommand
properties of theSqlDataSource
.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.
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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.