articles

Home / DeveloperSection / Articles / Repeater Control in ASP.Net with C#

Repeater Control in ASP.Net with C#

AVADHESH PATEL14184 24-Jul-2012

Repeater Control:

The Repeater control performs a very common function that most Web developers have encountered in their projects work. Very often you need to display records from a database, and most likely you would use a FOR loop to write the HTML code so that the records can be displayed within a table. Using the Repeater control, this process can very easily be automated.
A Repeater control is a light weight control which can be used for simple reporting purposes. It supports basic event-handling like In it, Load, Unload etc., This also supports some basic formatting of data and can be presented to the user. A Repeater control offers limited level of data editing or selecting capabilities. For such editing and updates ASP .Net offers DataList and DataGrid controls.
A Repeater control can be used to build small and Flexible reports with the templates for the items. It supports the following five templates.

Uses of Repeater Control:

Repeater Control is used to display repeated list of items that are bound to the control and it’s same as gridview and datagridview. Repeater control is lightweight and faster to display data when compared with gridview and datagrid. By using this control we can display data in custom format but it’s not possible in gridview or datagridview and it doesn’t support for paging and sorting.  

The Repeater control works by looping through the records in your data source and then repeating the rendering of its templates called item template. Repeater control contains different types of template fields those are,

1) ItemTemplate

2) AlternatingitemTemplate

3) HeaderTemplate

4) FooterTemplate

5) SeperatorTemplate 

ItemTemplate: ItemTemplate defines how the each item is rendered from data source collection.

AlternatingItemTemplate: AlternatingItemTemplates is used to change the background color and styles of AlternatingItems in DataSource collection 

HeaderTemplate: HeaderTemplate is used to display Header text for DataSource collection and apply different styles for header text.

FooterTemplate: FooterTemplate is used to display footer element for DataSource collection

SeparatorTemplate: SeparatorTemplate will determine separator element which separates each Item in Item collection. Usually, SeparateTemplate will be <br> html element or <hr> html element.

Demo on Repeater Control
Step1:-Create a table in SQL DataBase
CREATE TABLE [dbo].[gridview]
(
      [id] [int] IDENTITY(1,1)NOTNULL,
      [name] [varchar](50)NOTNULL,
      [age] [int] NOT NULL,
      [salary] [decimal](18, 0)NOTNULL,
      [country] [varchar](50)NOTNULL,
      [city] [varchar](50)NOTNULL
) 

Step2:- Drag and Drop Repeater Control on aspx page
Step3:- Design Repeater Control for display record according data table
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater ID="Repeater1" runat="server" >
 
    <HeaderTemplate>
    <table>
    <tr>
    <td style="background-color:Gray;width:100px;" >Id</td>
    <td style="background-color:Gray;width:100px;" >Name</td>
    <td style="background-color:Gray;width:100px;" >Age</td>
    <td style="background-color:Gray;width:100px;" >Salary</td>
    <td style="background-color:Gray;width:100px;" >Country</td>
    <td style="background-color:Gray;width:100px;" >City</td>
    </tr>
    </table>
    </HeaderTemplate>
 
    <ItemTemplate>
    <table>
    <tr>
    <td style="background-color:#FFFFFF;width:100px;" >
    <asp:Label ID="lblId" runat="server" Text='<%#Bind("id")%>'></asp:Label>
    </td>
    <td style="background-color:#FFFFFF;width:100px;" >
    <asp:Label ID="lblName" runat="server" Text='<%#Bind("name")%>'></asp:Label>
    </td>
    <td style="background-color:#FFFFFF;width:100px;" >
    <asp:Label ID="lblAge" runat="server" Text='<%#Bind("age")%>'></asp:Label>
    <td style="background-color:#FFFFFF;width:100px;" >
    <asp:Label ID="lblSalary" runat="server" Text='<%#Bind("salary")%>'></asp:Label>
    </td>
    <td style="background-color:#FFFFFF;width:100px;" >
    <asp:Label ID="lblCountry" runat="server" Text='<%#Bind("country")%>'></asp:Label>
    <td style="background-color:#FFFFFF;width:100px;" >
    <asp:Label ID="lblCity" runat="server" Text='<%#Bind("city")%>'></asp:Label>
    </tr>
    </table>  
    </ItemTemplate>
 
    </asp:Repeater>
   
    </div>
    </form>
</body>

 Design View of Repeater Control

Repeater Control in ASP.Net with C#

Step 4:- Bind the Repeater Control to DataTable

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string conString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
        SqlConnection con = new SqlConnection(conString);
        SqlDataAdapter adap = new SqlDataAdapter("select * from gridview", con);
        DataTable dt = new DataTable();
        adap.Fill(dt);
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
    }
}

 Step5:- Run the web page and see output

Repeater Control in ASP.Net with C#


Updated 07-Sep-2019
Avadhesh Kumar Patel District Project Manager - Aligarh 14 months work experience in Panchayati Raj Department Sector as District Project Manager & 12 months work experience in IT Sector as Software Engineer. :-)

Leave Comment

Comments

Liked By