Latest

random

How to use Repeater Control in ASP.Net C# with Example

Bind Data using Repeater Control in Asp.Net

The ASP.NET Repeater is a basic container control that allows you to create custom lists from any data available to the page. When the page runs, the Repeater control loops through the records in the data source and renders an item for each record.
  • A table layout
  • A comma-delimited list (for example, a, b, c, d, and so on)
  • An XML formatted list
  • ItemTemplate Contains the HTML elements and controls to render once for each data item in the data source.
  • AlternatingItemTemplate Contains the HTML elements and controls to render once for every other data item in the data source. Typically, this template is used to create a different look for the alternating items, such as a different background color than the color that is specified in the ItemTemplate.
  • HeaderTemplate and FooterTemplate Contains the text and controls to render at the beginning and end of the list, respectively.
  • SeparatorTemplate Contains the elements to render between each item (A typical example might be a line).

Advantages of repeater control over grid view in asp.net C#

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 functionalities of grid view to repeater but you have to do it manually.

So it depends upon you requirements either you need repeater or gridview.

ASP.Net Repeater Control Example

The Repeater control is a container control that allows you to create custom lists out of any data that is available to the page. The Repeater control does not have a built-in rendering of its own, which means that you must provide the layout for the Repeater control by creating templates. 

Repeater control, ASP. NET Repeater, Repeater Web server control, asp.net repeater control, example c# code, display the data using repeater control
repeater control in asp.net example c# code
In the following example I will show how to display data in asp.net repeater control with C#. Find the sample code below :-
Default.aspx
<%@ 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>C# - ASP.NET Repeater Control Example</title>
    <style type="text/css">
        .accordianContainer {
            max-height: 350px;
            overflow: auto;
        }
        /*----------------Data Grid inside Pop up----------------------*/
        .CompApp_TableScroll {
            max-height: 255px;
            overflow: auto;
        }

        .CompApp_DataTable {
            border-collapse: collapse;
        }

            .CompApp_DataTable tr th {
                background-color: #dedede;
                color: #333333;
                padding: 5px;
                border: 1px solid #cccccc;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
                font-weight: normal;
                white-space: nowrap;
            }

            .CompApp_DataTable tr:nth-child(2n+2) {
                background-color: #f3f4f5;
            }

            .CompApp_DataTable tr:nth-child(2n+1) td {
                background-color: #d6dadf;
                color: #454545;
            }

            .CompApp_DataTable tr td {
                padding: 5px;
                color: #454545;
                font-family: Arial, Helvetica, sans-serif;
                font-size: 11px;
                border: 1px solid #cccccc;
                vertical-align: middle;
                text-align: left;
                white-space: nowrap;
            }

                .CompApp_DataTable tr td:first-child {
                    text-align: left;
                    font-weight: bold;
                    text-align: left;
                }
        /*-------------------------*/
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="accordianContainer">

            <asp:Repeater ID="rEmp" runat="server">
                <HeaderTemplate>
                    <h5>Display Data in ASP.NET Repeater Control ..</h5>
                    <table class="CompApp_DataTable">
                        <tr>
                            <th><b>EmpId</b></th>
                            <th><b>Name</b></th>
                            <th><b>Address</b></th>
                        </tr>
                </HeaderTemplate>

                <ItemTemplate>
                    <tr>
                        <td>
                            <%# DataBinder.Eval(Container, "DataItem.EmpId")%>
                        </td>
                        <td>
                            <%# DataBinder.Eval(Container, "DataItem.Name")%>
                        </td>
                        <td>
                            <%# DataBinder.Eval(Container, "DataItem.Address")%>
                        </td>
                    </tr>
                </ItemTemplate>

                <FooterTemplate>
                    </table>

                    <h5>Footer ..</h5>
                </FooterTemplate>

            </asp:Repeater>
        </div>
    </form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindRepeaterControl();
        }
    }

    private void BindRepeaterControl()
    {
        try
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("EmpId", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Address", typeof(string));


            dt.Rows.Add(25, "Rk", "Gurgaon");
            dt.Rows.Add(50, "Sachin", "Noida");
            dt.Rows.Add(10, "Nitin", "Noida");
            dt.Rows.Add(21, "Aditya", "Meerut");
            dt.Rows.Add(100, "Mohan", "Banglore");

            if (dt.Rows.Count > 0)
            {
                rEmp.DataSource = dt;
                rEmp.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
I hope you will enjoy the development tip while programming with C# to bind data using asp.net repeater control and what are the advantages of using repeater control over gridview or datalist. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. 

Also If you like this article, don't forget to share this article with your friends and colleagues. 

1 comment:

All Rights Reserved by Etechpulse © 2015 - 2016
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.