<asp:Repeater> Control
The GridView, DetailsView, and FormView are new additions under the .NET 2.0 framework and
are likely to become the workhorses for delivery of information from external data sources.
Under earlier versions, three other bindable controls were used:
<asp:Repeater>, <asp:DataList>,
and <asp:DataGrid>. These controls still have a
place in ASP.NET 2.0, although most of their functionality has been absorbed by the newer
controls. You are also likely to encounter them on existing Web pages that have not been
transitioned to the new environment. Each of these controls are briefly covered in the
following tutorials.
An <asp:Repeater> control displays a repeated list of
data items that are bound to it. Like the GridView, it is designed for convenient display of records
from a database or other relational data source.
The Repeater control is based on the use of templates, models for output display using binding
expressions, server controls, and XHTML tags to structure the output. Its use is very similar to
that of a FormView or a TemplateField in a GridView. The general format for coding a Repeater is shown
below. Not all sections of the control are needed to produce simple data listings.
Figure 8-1. General format for <asp:Repeater> control.
Binding to a Repeater Control
A Repeater is bound to a data source to display its contents. As in the case of a GridView,
a Repeater can be linked to a data source control such as an AccessDataSource. As in the case of
using TemplateField columns in a GridView, data fields are bound to the Repeater through individual
data field binding expressions.
In its simplest format, the Repeater requires only an <Item Template>
to describe its output format. Within this template, text, XHTML, and binding
expressions are coded to provide a model for formatting the data. In the following example, output
is formatted as repeating lines of data, each line composed of selected data fields from the
Books table of the BooksDB.mdb database.
DB111,
Oracle Database,
$69.99
DB222,
Databases in Depth,
$29.95
DB333,
Database Processing,
$136.65
DB444,
Access Database Design,
$34.95
DB555,
SQL Server 2005,
$29.99
GR111,
Adobe Photoshop CS2,
$29.99
GR222,
Learning Web Design,
$39.95
GR333,
Macromedia Flash Professional,
$44.99
GR444,
Digital Photographer Handbook,
$24.95
GR555,
Creating Motion Graphics,
$59.95
HW111,
How Computers Work,
$29.99
HW222,
Upgrading and Repairing PCs,
$59.99
HW333,
USB System Architecture,
$49.99
HW444,
Designing Embedded Hardware,
$44.95
HW555,
Contemporary Logic Design,
$102.95
SW111,
Java How to Program,
$98.59
SW222,
C Programming Language,
$44.25
SW333,
Programming C#,
$44.95
SW444,
Programming PHP,
$39.95
SW555,
Visual Basic.NET Programming,
$49.99
SY111,
Operating System Concepts,
$95.75
SY222,
The UNIX Operating System,
$19.95
SY333,
Windows Server 2003,
$29.99
SY444,
Linux in a Nutshell,
$44.95
SY555,
Mastering Active Directory,
$49.99
WB111,
Ajax in Action,
$22.67
WB222,
Professional ASP.NET 2.0,
$32.99
WB333,
Cascading Style Sheets,
$39.95
WB444,
DOM Scripting,
$23.09
WB555,
Microsoft ASP.NET 2.0,
$29.99
Figure 8-2. Using a Repeater to display a database recordset.
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
SelectCommand="SELECT * FROM Books ORDER BY BookID"/>
<asp:Repeater id="BookTable" DataSourceID="BookSource" Runat="Server">
<ItemTemplate>
<%# Eval("BookID") %>,
<%# Eval("BookTitle") %>,
<%# String.Format("{0:C}", Eval("BookPrice")) %><br/>
</ItemTemplate>
</asp:Repeater>
Listing 8-1. Binding a data source to a Repeater.
The <ItemTemplate> section of the control specifies the data
items to be displayed along with any XHTML formatting for the data. This template is a model for
a single output line, and when data are bound to the Repeater the template is repeated for as
many records as appear in the recordset.
Data displays inside an ItemTemplate are given by binding expressions in the same format
used for other template-based controls.
Figure 8-3. General format for data binding expression for Repeater control.
The fieldname is the name of a field from the data source
whose value is displayed. In the above example, three data fieldsBookID
, BookTitle, and BookPriceare
extracted to display all records in the Books table. Note that data
binding can take place only in the <ItemTemplate>, not in the
<HeaderTemplate> or
<FooterTemplate> sections of the Repeater.
Although these simple binding expressions can work for a Repeater, it is more conventional to
bind output to server controls to take advantage of styling properties of the controls. An alternative
to coding the above Repeater is shown below using Label controls as display areas with output
bound to the Text properties of the controls.
<asp:Repeater id="BookTable" DataSourceID="BookSource" Runat="Server">
<ItemTemplate>
<asp:Label Text='<%# Eval("ItemNumber") %>' Runat="Server"/>,
<asp:Label Text='<%# Eval("ItemName") %>' Runat="Server"/>,
<asp:Label Text='<%# String.Format("{0:C}", Eval("BookPrice")) %>'
Runat="Server"/><br/>
</ItemTemplate>
</asp:Repeater>
Listing 8-2. Binding a data source to Repeater controls.
Formatting a Repeater as a Table
There can be three sections of Repeater output described within
<HeaderTemplate>, <ItemTemplate>, and
<FooterTemplate> sections of the control. The
HeaderTemplate appears one time at the beginning of output; the ItemTemplate is
repeated for as many records as there are in the bound data source; the FooterTemplate
appears one time at the close of output. These three sections are ideally suited for
displaying output within a table format.
Book Listing
| ID |
Title |
Price |
Qty |
Amount |
| DB111 |
Oracle Database |
$69.99 |
10 |
699.90 |
| DB222 |
Databases in Depth |
$29.95 |
6 |
179.70 |
| DB333 |
Database Processing |
$136.65 |
12 |
1,639.80 |
| DB444 |
Access Database Design |
$34.95 |
25 |
873.75 |
| DB555 |
SQL Server 2005 |
$29.99 |
0 |
0.00 |
| GR111 |
Adobe Photoshop CS2 |
$29.99 |
4 |
119.96 |
| GR222 |
Learning Web Design |
$39.95 |
8 |
319.60 |
| GR333 |
Macromedia Flash Professional |
$44.99 |
17 |
764.83 |
| GR444 |
Digital Photographer Handbook |
$24.95 |
22 |
548.90 |
| GR555 |
Creating Motion Graphics |
$59.95 |
13 |
779.35 |
| HW111 |
How Computers Work |
$29.99 |
8 |
239.92 |
| HW222 |
Upgrading and Repairing PCs |
$59.99 |
5 |
299.95 |
| HW333 |
USB System Architecture |
$49.99 |
1 |
49.99 |
| HW444 |
Designing Embedded Hardware |
$44.95 |
3 |
134.85 |
| HW555 |
Contemporary Logic Design |
$102.95 |
2 |
205.90 |
| SW111 |
Java How to Program |
$98.59 |
9 |
887.31 |
| SW222 |
C Programming Language |
$44.25 |
12 |
531.00 |
| SW333 |
Programming C# |
$44.95 |
0 |
0.00 |
| SW444 |
Programming PHP |
$39.95 |
17 |
679.15 |
| SW555 |
Visual Basic.NET Programming |
$49.99 |
13 |
649.87 |
| SY111 |
Operating System Concepts |
$95.75 |
1 |
95.75 |
| SY222 |
The UNIX Operating System |
$19.95 |
12 |
239.40 |
| SY333 |
Windows Server 2003 |
$29.99 |
25 |
749.75 |
| SY444 |
Linux in a Nutshell |
$44.95 |
14 |
629.30 |
| SY555 |
Mastering Active Directory |
$49.99 |
8 |
399.92 |
| WB111 |
Ajax in Action |
$22.67 |
14 |
317.38 |
| WB222 |
Professional ASP.NET 2.0 |
$32.99 |
21 |
692.79 |
| WB333 |
Cascading Style Sheets |
$39.95 |
6 |
239.70 |
| WB444 |
DOM Scripting |
$23.09 |
8 |
184.72 |
| WB555 |
Microsoft ASP.NET 2.0 |
$29.99 |
12 |
359.88 |
| Source: Books table |
Figure 8-4. Using a Repeater to display a recordset in table format.
<SCRIPT Runat="Server">
Function Get_Amount (Price As Decimal, Quantity As Integer)
Dim Amount As Decimal = Price * Quantity
Return String.Format("{0:#,0.00}", Amount)
End Function
</SCRIPT>
<form Runat="Server">
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
SelectCommand="SELECT * FROM Books ORDER BY BookID"/>
<asp:Repeater id="BookTable" DataSourceID="BookSource" Runat="Server">
<HeaderTemplate>
<table border="1" cellpadding="1" style="border-collapse:collapse">
<caption style="font-weight:bold">Book Listing</caption>
<tr style="background-color:#707070; color:#FFFFFF">
<th>ID</th>
<th>Title</th>
<th>Price</th>
<th>Qty</th>
<th>Amount</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label Text='<%# Eval("BookID") %>' Runat="Server"/></td>
<td><asp:Label Text='<%# Eval("BookTitle") %>' Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# String.Format("{0:N}", Eval("BookPrice")) %>'
Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# String.Format("{0:D}", Eval("BookQty")) %>'
Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# Get_Amount(Eval("BookPrice"), Eval("BookQty")) %>'
Runat="Server"/></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="background-color:#707070; color:#FFFFFF">
<td colspan="5" style="font-size:10pt; font-style:italic">
Source: Books table
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
Listing 8-3. Binding a data source to a Repeater with Template rows.
The <HeaderTemplate> contains HTML code to define a table and
describe its column headings, which are displayed once within the output. The
<ItemTemplate> contains code to describe a table row that is repeated once for
each record in the recordset. The <FooterTemplate> contains
code for the closing table tags that appear once at the end of output.
The BookPrice and BookQty values for a row
are passed to function Get_Amount where the amount is calculated and
formatted with a custom format string for return to the calling statement for display.
Other Templates
An <AlternatingItemTemplate> can be coded following the
<ItemTemplate> to decribe the appearance of alternating rows
of output. The AlternatingItemTemplate is a repeating template that can contain different data
items from the ItemTemplate. A <SeparatorTemplate> can be
coded to describe a separator line between each detail line of display. In the following example,
an AlternatingItemTemplate is added to the previous Repeater to produce alternating row shadings.
Book Listing
| ID |
Title |
Price |
Qty |
Amount |
| DB111 |
Oracle Database |
$69.99 |
10 |
699.90 |
| DB222 |
Databases in Depth |
$29.95 |
6 |
179.70 |
| DB333 |
Database Processing |
$136.65 |
12 |
1,639.80 |
| DB444 |
Access Database Design |
$34.95 |
25 |
873.75 |
| DB555 |
SQL Server 2005 |
$29.99 |
0 |
0.00 |
| GR111 |
Adobe Photoshop CS2 |
$29.99 |
4 |
119.96 |
| GR222 |
Learning Web Design |
$39.95 |
8 |
319.60 |
| GR333 |
Macromedia Flash Professional |
$44.99 |
17 |
764.83 |
| GR444 |
Digital Photographer Handbook |
$24.95 |
22 |
548.90 |
| GR555 |
Creating Motion Graphics |
$59.95 |
13 |
779.35 |
| HW111 |
How Computers Work |
$29.99 |
8 |
239.92 |
| HW222 |
Upgrading and Repairing PCs |
$59.99 |
5 |
299.95 |
| HW333 |
USB System Architecture |
$49.99 |
1 |
49.99 |
| HW444 |
Designing Embedded Hardware |
$44.95 |
3 |
134.85 |
| HW555 |
Contemporary Logic Design |
$102.95 |
2 |
205.90 |
| SW111 |
Java How to Program |
$98.59 |
9 |
887.31 |
| SW222 |
C Programming Language |
$44.25 |
12 |
531.00 |
| SW333 |
Programming C# |
$44.95 |
0 |
0.00 |
| SW444 |
Programming PHP |
$39.95 |
17 |
679.15 |
| SW555 |
Visual Basic.NET Programming |
$49.99 |
13 |
649.87 |
| SY111 |
Operating System Concepts |
$95.75 |
1 |
95.75 |
| SY222 |
The UNIX Operating System |
$19.95 |
12 |
239.40 |
| SY333 |
Windows Server 2003 |
$29.99 |
25 |
749.75 |
| SY444 |
Linux in a Nutshell |
$44.95 |
14 |
629.30 |
| SY555 |
Mastering Active Directory |
$49.99 |
8 |
399.92 |
| WB111 |
Ajax in Action |
$22.67 |
14 |
317.38 |
| WB222 |
Professional ASP.NET 2.0 |
$32.99 |
21 |
692.79 |
| WB333 |
Cascading Style Sheets |
$39.95 |
6 |
239.70 |
| WB444 |
DOM Scripting |
$23.09 |
8 |
184.72 |
| WB555 |
Microsoft ASP.NET 2.0 |
$29.99 |
12 |
359.88 |
| Source: Books table |
Figure 8-5. Using a Repeater with an AlternatingItemTemplate.
<SCRIPT Runat="Server">
Function Get_Amount (Price As Decimal, Quantity As Integer)
Dim Amount As Decimal = Price * Quantity
Return String.Format("{0:#,0.00}", Amount)
End Function
</SCRIPT>
<form Runat="Server">
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
SelectCommand="SELECT * FROM Books ORDER BY BookID"/>
<asp:Repeater id="BookTable" DataSourceID="BookSource" Runat="Server">
<HeaderTemplate>
<table border="0" cellpadding="1">
<caption style="font-weight:bold">Book Listing</caption>
<tr style="background-color:#707070; color:#FFFFFF">
<th>ID</th>
<th>Title</th>
<th>Price</th>
<th>Qty</th>
<th>Amount</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label Text='<%# Eval("BookID") %>' Runat="Server"/></td>
<td><asp:Label Text='<%# Eval("BookTitle") %>' Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# String.Format("{0:N}", Eval("BookPrice")) %>'
Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# String.Format("{0:D}", Eval("BookQty")) %>'
Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# Get_Amount(Eval("BookPrice"), Eval("BookQty")) %>'
Runat="Server"/></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#A0A0A0; color:#FFFFFF">
<td><asp:Label Text='<%# Eval("BookID") %>' Runat="Server"/></td>
<td><asp:Label Text='<%# Eval("BookTitle") %>' Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# String.Format("{0:N}", Eval("BookPrice")) %>'
Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# String.Format("{0:D}", Eval("BookQty")) %>'
Runat="Server"/></td>
<td style="text-align:right">
<asp:Label Text='<%# Get_Amount(Eval("BookPrice"), Eval("Bookqty")) %>'
Runat="Server"/></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
<tr style="background-color:#707070; color:#FFFFFF">
<td colspan="5" style="font-size:10pt; font-style:italic">
Source: Books table
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
Listing 8-4. Coding a Repeater with various template rows.
The <asp:Repeater> control provides great flexibility in
formatting output from a bound data source. Virtually any XHTML tags and server controls can be
added to the templates to arrange and style the data items. You will probably favor this control
if you prefer, or if you don't mind, coding XHTML to produce your output.
A Repeater does not provide automatic sorting as do the GridView, DetailsView, and FormView
controls. This feature must be scripted. Sorting records for a Repeater is discussed under the
topic "Displaying Sorted Records." Neither does a Repeater offer built-in ways of paging output.
You would need to write special script to cause this to happen.