Display data with Repeater control in ASP.NET

Category > ASP.NET || Published on : Thursday, October 9, 2014 || Views: 10525 || Asp.Net Repeater Repeater Server Control Display Data using Repeater


The Repeater control is a ASP.NET Data control that allows you to create custom lists out of any data that is available to the page. In Repeater control doesn’t have built in rendering of its own so we have to provide the layout for the Repeater control. Repeater control uses following Templates,

Repeater has 5 inline template to format it:

1.
2.
3.
4.
5.
6.

HeaderTemplate: This template is used for elements that you want to render once before your ItemTemplate section.

FooterTemplate:  This template is used for elements that you want to render once after your ItemTemplate section.

ItemTemplate: This template is used for elements that are rendered once per row of data. It is used to display records

AlternatingItemTemplate: This template is used for elements that are rendered every second row of data. This allows you to alternate background colors. It works on even number of records only.

SeperatorTemplate: It is used for elements to render between each row, such as line breaks.


 Advantages of Repeater Control over GridView in ASP.Net:-
In simple words we can say performance of repeater is far better than gridview. If you need basic rendering for read only items then its better to use repeater and if you need events , pagination and editable controls then you should go for gridview.

Simpler controls with less in built functionality are speedy. you can do implement all functionality of grid view to repeater but you have to do it manually. So it depends upon you requirements either you need repeater or gridview. For example find the source code below:-


Bind Repeater Control:

It’s quite easy to bind Repeater Control. When the page runs, the Repeater control loops through the records in the data source and renders an item for each record.

Note: for sample application, I have used the "NorthWind" Database. You c an download from the Microsoft website.

HTML Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

"http://www.w3.org/1999/xhtml">
"Head1" runat="server">
    Repeater control example


    "form1" runat="server">
    
"Repeater1" runat="server"> "1" cellspacing="1" width="50%" style="font-family: Calibri; border: 1px solid #C0C0C0; background-color: #D8D8D8"> "background-color: Gray; color: White"> "background-color: White"> "background-color: #AED6FF">
Employee-ID First Name Last Name
<%#DataBinder.Eval(Container, "DataItem.EmployeeID")%> <%#DataBinder.Eval(Container, "DataItem.FirstName")%> <%#DataBinder.Eval(Container, "DataItem.LastName")%>
<%#DataBinder.Eval(Container, "DataItem.EmployeeID")%> <%#DataBinder.Eval(Container, "DataItem.FirstName")%> <%#DataBinder.Eval(Container, "DataItem.LastName")%>


Code Behind:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    protected void BindData()
    {
        DataSet ds = new DataSet();
        conn.Open();

        string cmdstr = "Select * from Employees";
        SqlCommand cmd = new SqlCommand(cmdstr, conn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(ds);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
        conn.Close();
    }
}


For more details about repeater control refer to http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater(v=vs.110).aspx

 

Download Source Codes