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.
- 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.
- insert into Student_Details1 values(1,'yatendra',120,'1992-10-10','2013-12-10','MCA');
- insert into Student_Details1 values(2,'Rahul',125,'1993-05-07','2013-12-10','MBA');
- insert into Student_Details1 values(3,'sachin',120,'1992-08-02','2013-12-08','B-Tech');
- 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.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Repeater</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:Repeater ID="Repeater1" runat="server">
- <ItemTemplate>
- <div>
- <table>
- <tr>
- <th>Student<%#Eval("S_ID")%></th>
- </tr>
- <tr>
- <td>Student Name</td>
- <td><%#Eval("Student_Name")%></td>
- </tr>
- <tr>
- <td>Registration Number</td>
- <td><%#Eval("Register_No")%></td>
- </tr>
- <tr>
- <td>Date Of Birth</td>
- <td><%#Eval("D_O_B")%></td>
- </tr>
- <tr>
- <td>Date Of Examination</td>
- <td><%#Eval("D_O_E")%></td>
- </tr>
- <tr>
- <td>Department</td>
- <td><%#Eval("Department")%></td>
- </tr>
- </table>
- </div>
- </ItemTemplate>
- </asp:Repeater>
- </form>
- </body>
- </html>
Step 7
Now we will write the following code for the page_load.
- protected void Page_Load(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection("Data Source=MCNDESKTOP34;Initial Catalog=yatendra;Persist Security Info=True;User ID=sa;
Password=Password$2");
- SqlDataAdapter sda = new SqlDataAdapter("select * from Student_Details1",con);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- Repeater1.DataSource = dt;
- Repeater1.DataBind();
- }
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.