The ASP.NET Repeater Control

Exclusive offer: get 50% off this eBook here
ASP.NET Data Presentation Controls Essentials

ASP.NET Data Presentation Controls Essentials — Save 50%

Master the standard ASP.NET server controls for displaying and managing data

$23.99    $12.00
by Joydip Kanjilal | January 2008 | .NET Microsoft

The Repeater control is a data-bound control that uses templates to display data. The Repeater control works by looping through the records in your data source and then repeating the rendering of one of its templates called the ItemTemplate, one that contains the records that the control needs to render. In this article by Joydip Kanjilal we will learn more about the Repeater control and some of its uses.

The Repeater control in ASP.NET is a data-bound container control that can be used to automate the display of a collection of repeated list items. These items can be bound to either of the following data sources:

  • Database Table
  • XML File

In a Repeater control, the data is rendered as DataItems that are defined using one or more templates. You can even use HTML tags such as <li>, <ul>, or <div> if required. Similar to the DataGrid, DataList, or GridView controls, the Repeater control has a DataSource property that is used to set the DataSource of this control to any ICollection, IEnumerable, or IListSource instance. Once this is set, the data from one of these types of data sources can be easily bound to the Repeater control using itsDataBind() method.

However, the Repeater control by itself does not support paging or editing of data. The Repeater control is light weight and does not contain so many features as the DataGrid contains. However, it enables you to place HTML code in its templates. It is great in situations where you need to display the data quickly and format the data to be displayed easily.

Using the Repeater Control

The Repeater control is a data-bound control that uses templates to display data. It does not have any built-in support for paging, editing, or sorting of the data that is rendered through one or more of its templates. The Repeater control works by looping through the records in your data source and then repeating the rendering of one of its templates called the ItemTemplate, one that contains the records that the control needs to render.

To use this control, drag and drop the control in the design view of the web form onto a web form from the toolbox. Refer to the following screenshot:

The ASP.NET Repeater Control

You can also drag and drop the Repeater control from the toolbox onto the source view directly. This is shown in the following screenshot:

The ASP.NET Repeater Control

For customizing the behavior of this control, you have to use the built-in templates that this control comes with. These templates are actually blocks of HTML code. The Repeater control contains the following five templates:

  1. HeaderTemplate
  2. ItemTemplate
  3. AlternatingItemTemplate
  4. SeparatorTemplate
  5. FooterTemplate

The following screenshot shows how a Repeater control looks when populated with data.

The ASP.NET Repeater Control

Note that the templates (Header, Item, Footer, Alternate and Separator) have all been used.

The following code snippet is an example of the order in which the templates of the Repeater control are used.

<asp:Repeater id="repEmployee" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:Repeater>

When the Repeater control is bound to a data source, the data from the data source is displayed using the ItemTemplate element and any other optional elements, if used. Note that the contents of the HeaderTemplate and the FooterTemplate are rendered once for each Repeater control. The contents of the ItemTemplate are rendered for each record in the control.

You can also use the additional AlternatingItemTemplate element after the ItemTemplate element for specifying the appearance of each alternate record. You can also use the SeparatorTemplate element between each record for specifying the separators for the records.

Displaying Data Using the Repeater Control

This section discusses how we can display data using the Repeater control. As discussed earlier, the Repeater control uses templates for formatting the data that it displays. The following code snippet displays the code in an .aspx file that contains a Repeater control.

Note that we would be making use of templates and that the data would be bound to the control from the code-behind file using the DataManager class.

The Repeater control is populated with data in the Page_Load event by reusing the DataManager().

Note how the SeparatorTemplate and the AlternatingItemTemplate have been used in the previous code example. Further, the DataBinder.Eval() method has been used to display the values of the corresponding fields from the data container, (in our case, the DataSet instance) in the Repeater control. The FooterTemplate uses the Total Records variable and substitutes its value to display the total number of records displayed by the control.

The following is the output on execution.

The ASP.NET Repeater Control

The Header and the Footer templates of the Repeater control are still rendered even if the data source does not contain any data. If you want to suppress their display, you can use the Visible property of the Repeater control and use it to suppress the display of these templates with a simple logic. Here is how you specify the Visible property of this control in your .aspx file to achieve this:Visible="<%# Repeater1.Items.Count > 0 %>"When you specify the Visible property as shown here, the Repeater is made visible only if there are records in your data source.

 

Displaying Checkboxes in a Repeater Control

Let us now understand how we can display checkboxes in a Repeater Control and retrieve the number of checked items. We will use a Button control and a Label control in our page. When you click on the Button control, the number of checked items in the Repeater Control will be displayed in the Label control. The output on execution is similar to what is shown in the following screenshot:

The ASP.NET Repeater Control

Here is the code that we will use in the .aspx file to display checkboxes in a Repeater control.

The data is bound to the Repeater control in the Page_Load event as follows:

Note that we have used the Page.IsPostBack to check whether the page has posted back in the Page_Load method. If you don't bind data by checking whether the page has posted back, the Repeater control will be rebound to data once again after a postback and all the checkboxes in your web page will be reset to the unchecked state.

The source code for the click event of the Button control that we have used is as follows:

When you execute the application, the Repeater control is displayed with records from the employee table. Now you check one or more of the checkboxes and then click on the Button control just beneath the Repeater control as follows:

The ASP.NET Repeater Control

Note that the number of checked records is displayed in the Label Control.

Summary

This article discussed the Repeater control and how we can use it in ourASP.NET applications. Though this control does not support all the functionalities of other data controls, like DataGrid and GridView, it is still a good choice if you want faster rendering of data as it is light weight, and is very flexible.

ASP.NET Data Presentation Controls Essentials Master the standard ASP.NET server controls for displaying and managing data
Published: December 2007
eBook Price: $23.99
Book Price: $39.99
See more
Select your format and quantity:

About the Author :


Joydip Kanjilal

Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.NET, a speaker, and the author of several books and articles. He has over 16 years of experience in the IT industry, with more than 10 years using Microsoft .NET and its related technologies. He was selected as the MSDN Featured Developer of the Fortnight a number of times and also as the Community Credit Winner by www.communitycredit. com several times. He has authored the following books:

  • Visual Studio Six in One (Wrox Publishers)
  • ASP.NET 4.0 Programming (Mc-Graw Hill Publishing)
  • Entity Framework Tutorial (Packt Publishing)
  • Pro Sync Framework (APRESS)
  • Sams Teach Yourself ASP.NET Ajax in 24 Hours (Sams Publishing)
  • ASP.NET Data Presentation Controls Essentials (Packt Publishing)

He has also authored more than 250 articles for some of the most reputable sites, such as www.msdn.microsoft.com, www.code-magazine.com, www.asptoday.com, www.devx.com, www.ddj.com, www.aspalliance.com, www.aspnetpro.com, www.sql-server-performance.com, and www.sswug.com. A lot of these articles have been selected at www.asp.net—Microsoft's official website on ASP.NET.

He has years of experience in designing and architecting solutions for various domains. His technical strengths include C, C++, VC++, Java, C#, Microsoft .NET, Ajax, WCF, REST, SOA, Design Patterns, SQL Server, Operating Systems, and Computer Architecture.

For more details, please refer to the following links:

Books From Packt

ASP.NET Data Presentation Controls Essentials
ASP.NET Data Presentation Controls Essentials

ODP.NET Developer’s Guide: Oracle Database 10g Development with Visual Studio 2005 and the Oracle Data Provider for .NET
ODP.NET Developer’s Guide: Oracle Database 10g Development with Visual Studio 2005 and the Oracle Data Provider for .NET

Beginners Guide to SQL Server Integration Services Using Visual Studio 2005
Beginners Guide to SQL Server Integration Services Using Visual Studio 2005

DotNetNuke Skinning Tutorial
DotNetNuke Skinning Tutorial

Visual SourceSafe 2005 Software Configuration Management in Practice
Visual SourceSafe 2005 Software Configuration Management in Practice

SharePoint Designer Tutorial: Working with SharePoint Websites
SharePoint Designer Tutorial: Working with SharePoint Websites

Building Websites with VB.NET and DotNetNuke 4
Building Websites with VB.NET and DotNetNuke 4

Programming Microsoft® Dynamics™ NAV
Programming Microsoft® Dynamics™ NAV


No votes yet
how to create cheakbox by
please help me

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
k
Z
X
7
X
n
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software