Introduction:

Here we will explain how to use Repeater in asp.net or what is Repeater in asp.net or how to bind data to the Repeater in asp.net with example.

Description:

Asp.Net Repeater control is a container control which is used to display the repeated list of items. It’s allows you to create custom lists for display the data on the page. It’s also support Data binding using the Data Source property, which allows you to bind to various objects, including ADO.NET datasets or data table or System.Collections.ArrayList object.

The Repeater control contains different types of template fields:

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.

Example:

 Repeater.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Repeater.aspx.cs" 
                                           Inherits="AspDotNet.Repeater" %>
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>Repeter Control in ASP.NET</h3>    
    <div>
    <asp:Repeater ID="RepterDetails" runat="server">
    <HeaderTemplate>
    <table style="border:1px solid #0000FF; width:500px" cellpadding="0">
    <tr style="background-color:#FF6600; color:#000000; font-size: large; font-weight: bold;">
    <td colspan="2">
    <b>Comments</b>
    </td>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr style="background-color:#EBEFF0">
    <td>
    <table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >
    <tr>
    <td >
    ID:
    <asp:Label ID="lblId" runat="server" Text='<%#Eval("EmployeeID") %>' Font-Bold="true"/>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td>
    By' <asp:Label ID="lblName" runat="server" Text='<%#Eval("EpmloyeeName") %>'/>
    </td>
    </tr>
    <tr>
    <td>
    Comment: <asp:Label ID="lblComment" runat="server" Text='<%#Eval("Comment") %>'/>
    </td>
    </tr>
    <tr>
    <td colspan="2">&nbsp;</td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    </table>
    </FooterTemplate>
    </asp:Repeater>
    </div>
    </form>
</body>
</html>


Repeater.aspx.cs:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AspDotNet
{
    public partial class Repeater : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ArrayList list = new ArrayList();
                Employee emp = new Employee();
                emp.EmployeeID = "EMPID0001";
                emp.EpmloyeeName = "Johen Mark";
                emp.Comment ="Reference site about Lorem Ipsum,"+
                "giving information on its origins, as well as a random Lipsum generator.";
                list.Add(emp);
                emp = new Employee();
                emp.EmployeeID = "EMPID0002";
                emp.EpmloyeeName = "Mahesh Patel";
                emp.Comment = "Reference site about Lorem Ipsum," +
                "giving information on its origins, as well as a random Lipsum generator.";
                list.Add(emp);
                emp = new Employee();
                emp.EmployeeID = "EMPID0003";
                emp.EpmloyeeName = "Rohan Roy";
                emp.Comment = "Reference site about Lorem Ipsum," +
                "giving information on its origins, as well as a random Lipsum generator.";
                list.Add(emp);
               
                RepterDetails.DataSource =list;
                RepterDetails.DataBind();
            }
        }
        class Employee
        {
            public string EmployeeID { get; set; }
            public string EpmloyeeName { get; set; }
            public string Comment { get; set; }
        }
    }
}

When you run Above example code Output will be shown like below:

Use Repeater Control in Asp.Net

I Hope this Article Will Help to you.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like