Introduction

Repeater is a data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list. In this article, I will demonstrate how to dynamically bind repeater control in ASP.NET from a database.

Step 1

Create a database table in SQL Server 2014.

  1. CREATE TABLE [dbo].[Employees](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [nvarchar](50) NULL,
  4. [Position] [nvarchar](50) NULL,
  5. [Office] [nvarchar](50) NULL,
  6. [Salary] [nvarchar](50) NULL,
  7. [Profile_Picture] [nvarchar](50) NULL,
  8. CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
  9. (
  10. [ID] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. GO
  1. CREATE PROCEDURE [dbo].[spGetEmployees]
  2. AS
  3. BEGIN
  4. SELECT Name,Position,Office,Salary,Profile_Picture from [dbo].[Employees]
  5. END

ASP.NET

Step 2

Open Visual Studio 2015, click on New Project, and create an empty web application project.

Screenshot for creating new project 1

ASP.NET
After clicking on New Project, one window will appear. Select Web from the left panel, choose ASP.NET Web Application, give a meaningful name to your project, then click on OK.

Screenshot for creating new project 2

ASP.NET

After clicking on OK, one more window will appear. Choose Empty, check on Web Forms checkbox, and click on OK.

Screenshot for creating new project 3

ASP.NET
Step 3

Double click on webconfig file and connect the database by writing the following line of code.
  1. <connectionStrings>
  2. <add name="DBCS" connectionString="data source=DESKTOP-M021QJH\SQLEXPRESS; database=DemoDB; integrated security=true;"/>
  3. </connectionStrings>

Step 4 - Add web form in your project

Right click on project, select Add >>Web Form.

Screenshot for adding web form 1

ASP.NET

After clicking on web form, one window will appear. Give name to the web form, such as - Repeater or any meaningful name. Click on OK and web form will be added to the project.

Screenshot for adding web form 2

ASP.NET
Step 5

Add script and styles of bootstrap in the head section of the web form.
  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

Step 6

Drag and drop a Repeater control on the web form. Design Repeater control.

  1. <body>
  2. <form id="form1" runat="server">
  3. <div class="container py-4">
  4. <h5 class="text-center text-uppercase">Repeater control in asp.net</h5>
  5. <asp:Repeater ID="Repeater1" runat="server">
  6. <ItemTemplate>
  7. <table class="table table-bordered">
  8. <tr>
  9. <td style="width: 20%;">
  10. <img alt="" width="150" class="img-thumbnail" src='<%#Eval("Profile_Picture")%>' />
  11. </td>
  12. <td>
  13. <table class="table">
  14. <tr>
  15. <td style="width:50%;">Employee Name:</td>
  16. <td><%#Eval("Name")%></td>
  17. </tr>
  18. <tr>
  19. <td>Position:</td>
  20. <td><%#Eval("Position")%></td>
  21. </tr>
  22. <tr>
  23. <td>Office:</td>
  24. <td><%#Eval("Office")%></td>
  25. </tr>
  26. <tr>
  27. <td>Salary:</td>
  28. <td><%#Eval("Salary")%></td>
  29. </tr>
  30. </table>
  31. </td>
  32. </tr>
  33. </table>
  34. </ItemTemplate>
  35. </asp:Repeater>
  36. </div>
  37. </form>
  38. </body>

Complete web form code

  1. <!DOCTYPE html>
  2. <html>
  3. <head runat="server">
  4. <title>Repeator</title>
  5. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  7. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div class="container py-4">
  12. <h5 class="text-center text-uppercase">Repeater control in asp.net</h5>
  13. <asp:Repeater ID="Repeater1" runat="server">
  14. <ItemTemplate>
  15. <table class="table table-bordered">
  16. <tr>
  17. <td style="width: 20%;">
  18. <img alt="" width="150" class="img-thumbnail" src='<%#Eval("Profile_Picture")%>' />
  19. </td>
  20. <td>
  21. <table class="table">
  22. <tr>
  23. <td style="width:50%;">Employee Name:</td>
  24. <td><%#Eval("Name")%></td>
  25. </tr>
  26. <tr>
  27. <td>Position:</td>
  28. <td><%#Eval("Position")%></td>
  29. </tr>
  30. <tr>
  31. <td>Office:</td>
  32. <td><%#Eval("Office")%></td>
  33. </tr>
  34. <tr>
  35. <td>Salary:</td>
  36. <td><%#Eval("Salary")%></td>
  37. </tr>
  38. </table>
  39. </td>
  40. </tr>
  41. </table>
  42. </ItemTemplate>
  43. </asp:Repeater>
  44. </div>
  45. </form>
  46. </body>
  47. </html>

Step 7

Right click and view web form code. Write the following code.

  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. namespace BindDataControl_Demo
  6. {
  7. public partial class Repeator : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. if (!IsPostBack)
  12. {
  13. BindRepeator();
  14. }
  15. }
  16. private void BindRepeator()
  17. {
  18. string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
  19. using (SqlConnection con = new SqlConnection(CS))
  20. {
  21. SqlCommand cmd = new SqlCommand("spGetEmployees", con);
  22. cmd.CommandType = CommandType.StoredProcedure;
  23. con.Open();
  24. Repeater1.DataSource = cmd.ExecuteReader();
  25. Repeater1.DataBind();
  26. }
  27. }
  28. }
  29. }

Step 8

Create a folder with name images in the project and add some images to the folder.

Step 9

Run the project by pressing Ctrl+F5.

Screenshot

ASP.NET

Conclusion

In this article, I have explained how to dynamically bind Repeater control from database step by step. I hope it will help you.