Populer Topics

Repeater Control

This control is also used to display data in tabular format. The most important thing not about this control does not have any view.

If you want display the data in tabular format like GridView, Repeater control, give us the functionality the data in customize format as per choice.

We need to write Html code of our choice. Repeater control support following templates.

<HeaderTemplate>
<ItemTemplate>
<AlternatingItemTemplate>
<SepratorTemplate>
<FooterTemplate>

<HeaderTemplate>

This control is used to heading of the repeater control.This control is executed only once we display any row we cannot bind data inside this template.

<ItemTemplate>

This Template is used to specified Formatting for data being display this is used format rows which will be display under this control. This template called each time the new row comes under repeater control.

<AlternattingItemTemplate>

This template is used to specifies alternating formatting control. This is also used for format rows. Which will be displayed under repeater control.

If we specifies this template odd rows comes under format of ItemTemplate and all even row comes under this template.

<SepratorTemplate>

This template is used to species format to separate each row of repeater control.This format comes between row of the repeater control as this is used to separate rows.

<FooterTemplate>

This template is used to set footer all the data display in footer. This template also contains any data bound information.Repeater control provide following property.

 

Property Description
DataSource

We can specifies data set or datatable as data source. This is runtime property.

HeaderTemplate

Allow us to specifies header template information.

ItemTemplate

Allow us to specifies ItemTemplate information.

Items

Contains item collection object each of data inside repeater control.

 

.Aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Codeplanex.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1">
            <asp:Repeater ID="rptList" runat="server">
                <HeaderTemplate>
                    <thead>
                        <tr>
                            <th>
                                Name
                            </th>
                            <th>
                                Mobile
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <%# Eval("Name") %>
                        </td>
                        <td>
                            <%# Eval("Mobile") %>
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </tbody>
                </FooterTemplate>
            </asp:Repeater>
        </table>
    </div>
    </form>
</body>
</html>

.Cs File

using System;
using System.Data.SqlClient;

namespace Codeplanex
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        SqlConnection con=new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Demo;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "Select * from CustomerMaster";
                con.Open();
                
                rptList.DataSource = cmd.ExecuteReader();
                rptList.DataBind();
                con.Close();
                
            }
        }
    }
}