In this tutorial we will learn how to create and use repeaters in ASP.NET.

Introduction

In this tutorial we will learn how to create and use repeaters in ASP.NET. Repeaters can be useful when dealing with information in SQL Databases, especially if it is a dynamic amount of information that you are grabbing. Gridviews can be used as an alternative to Repeaters, and they generally have a more formatted table like style.

Step 1. Setup the Project and Create the Repeater

1. Create a new Project in Visual Web Developer
2. Create a new Web From named “Default.aspx”
3. Create a new SQL Database and name it “Repeater.mdf”
4. Inside that Database create a Table called “Repeater” with the Columns ID(Primary Key, Identity), and Name(varchar(50))
5. Fill the Table out with some sample Data by right clicking it and selecting Show Table Data
6. Now go onto the form and drag an SQL data source object onto the form
7. Setup the SQL data source to connect to the Database and Table we just created by clicking on the Smart Tag and selecting Configure DataSource

Step 2. Linking the Repeater and the Database

1. Drag a Repeater Control from the Data Section of the Toolbar onto the Form
2. Click on the Smart Tag and select the Data Source we just created (“SqlDataSource1” for me)
3. Unlike a Gridview the Repeater requires a few extra steps to setup. Inside the tag in the code view we need to create an item template so that it know how to lay the information out. We use code nuggets to represent our Data inside the ItemTemplate. The following example will place the two Items grabbed each pass a few spaces apart then add a new line.

the code nuggets are represented by <%# %> this lets the repeater know that something from the database will be placed here dynamically.
4. We can now run the project and view how the Repeater displays our Data.

Conclusion

In this tutorial we learned how to create and setup a Repeater Control in ASP.NET. The repeater Control is very useful and dynamic in how it is setup. In ASP.NET there is another Control Called the GridView Control that can achieve similar results to the Repeater in a more uniform table like style.

Download Source Files