ASP.Net Repeater Control Using C#

The main use of Repeater Control is for displaying a repeated list of items bound to the control. A Repeater Control is faster and lightweight for displaying data compared to a GridView or DataGrid. With the Repeater control we can display data in a custom format. The main drawback of a Repeater Control is that it doesn't support paging and sorting.

The Repeater Control has the following types of template fields:

  • Item Template
  • AlternatingItem Template
  • Header Template
  • Footer Template
  • Separator Template

Item Template

The Item Template is useful for describing how each item will be rendered from the data source collection.

AlternatingItem Template

The AlternatingItem Template describes the markup for each item in the Repeater Control but for the AlternatingItems from the DataSource Collection such as different color and styles in the Repeater Control.

HeaderTemplate

The HeadderTemplate is basically useful for displaying a footer element for the DataSource Collection.

Footer Collection

The Footer Collection is useful for displaying a footer element for the DataSource Collection.

SeparatorTemplate

The SeparatorTemplate describes or determines the separator element that separates each item.

Step 1

Start Visual Studio.


Figure 1: Start Visual Studio 

Step 2

Now we need to create a website using "File" -> "New" -> "Website".


Figure 2: Create Website 

Step 3

Now, select the ASP.NET Empty Website and click on the Ok button.


Figure 3: ASP.NET Empty Website

Step 4

Now, add the Web form by right-clicking on the website and provide a name for the web form.


Figure 4: Add the Web Form

Step 5

Now we need to create a table in the SQL Server.
  1. create table Student_Details1(S_ID int Primary key, Student_name Varchar(50),Register_No int,D_O_B date, D_O_E date,Department varchar(50));  

Now we will insert the data into the Student_Details table.

  1. insert into Student_Details1 values(1,'yatendra',120,'1992-10-10','2013-12-10','MCA');  
  2. insert into Student_Details1 values(2,'Rahul',125,'1993-05-07','2013-12-10','MBA');  
  3. insert into Student_Details1 values(3,'sachin',120,'1992-08-02','2013-12-08','B-Tech');  
  4. insert into Student_Details1 values(4,'Rohit',120,'1992-05-15','2013-12-10','MCA');  

Step 6

Now we will write the following code for the Default.aspx form.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html>  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>Repeater</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.         <asp:Repeater ID="Repeater1" runat="server">  
  11.             <ItemTemplate>  
  12.                 <div>  
  13.                     <table>  
  14.                         <tr>  
  15.                             <th>Student<%#Eval("S_ID")%></th>  
  16.                         </tr>  
  17.                         <tr>  
  18.                             <td>Student Name</td>  
  19.                             <td><%#Eval("Student_Name")%></td>  
  20.                         </tr>  
  21.                         <tr>  
  22.                             <td>Registration Number</td>  
  23.                             <td><%#Eval("Register_No")%></td>  
  24.                         </tr>  
  25.                         <tr>  
  26.                             <td>Date Of Birth</td>  
  27.                             <td><%#Eval("D_O_B")%></td>  
  28.                         </tr>  
  29.                         <tr>  
  30.                             <td>Date Of Examination</td>  
  31.                             <td><%#Eval("D_O_E")%></td>  
  32.                         </tr>  
  33.                         <tr>  
  34.                             <td>Department</td>  
  35.                             <td><%#Eval("Department")%></td>  
  36.                         </tr>  
  37.                     </table>  
  38.                 </div>  
  39.             </ItemTemplate>  
  40.         </asp:Repeater>  
  41.     </form>  
  42. </body>  
  43. </html>  

Step 7

Now we will write the following code for the page_load.

  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         SqlConnection con = new SqlConnection("Data Source=MCNDESKTOP34;Initial Catalog=yatendra;Persist Security Info=True;User ID=sa;
            Password=Password$2"
    );  
  4.         SqlDataAdapter sda = new SqlDataAdapter("select * from Student_Details1",con);  
  5.         DataTable dt = new DataTable();  
  6.         sda.Fill(dt);  
  7.         Repeater1.DataSource = dt;  
  8.         Repeater1.DataBind();  
  9.     }  

Step 8

Now we will execute the website by using the F5 key and check that the data is fetched into the web form or not.


Figure 5: Output Window

So we have fetched the data from the database successfully.

Summary

In this article, I explained how to use the Repeater Control in your projects and fetch the data in a Repeater Control from the database.

I hope this article will be helpful for the beginners if they want to use the Repeater Control to make the project more efficient.

Up Next
    Ebook Download
    View all
    Learn
    View all