Categories
Asp, Asp.net

Repeater Control Example In Asp.Net

Repeater Control Example In Asp.Net

In this tutorial i will let you know, Repeater Control Example In Asp.Net. Repeater Control in Asp.Net is used to display repeated list of items.It displays records same like GidView and Datagrid, But as compare to Datagrid and Gridview it is much faster and lighweight.In Repeater we can format the style of displaying or data.

Repeater Control have 5 entity which you can say templates.

1. Header Template  2. Item Template  3. AlternatingItemTemplate  4. Seperator Template  5. Footer Template

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

2. AlternatingItemTemplate: AlternatingItemTemplates is used to change the styles of Alternate Items in Data Source collection

3. Header Template: Header Template is used for Header text for Data Source collection and apply styles for header text.

4. Footer Template: Footer Template is used to display footer element for Data Source collection.

5. SeparatorTemplate: SeparatorTemplate will determine separator element which separates each Item in Item collection.

Design View:

<%@ 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>Hightechnology Repeater Control Example</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:800px; margin:0 auto;">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table style="border:solid 1px black">
<tr>
<td style="width:100px">
EMP ID
</td>
<td style="width:100px">
Emp Name
</td>
<td style="width:100px">
Salary
</td>
<td style="width:100px">
Designation
</td>
</tr>
</table>
</HeaderTemplate>

<ItemTemplate>
<table style="border:solid 1px black">
<tr>
<td style="width:100px">
<asp:Label ID="empid" Text='<%#Eval("empno") %>' runat="server"></asp:Label>
</td>
<td style="width:100px">
<asp:Label ID="empname" Text='<%#Eval("ename") %>' runat="server"></asp:Label>
</td>
<td style="width:100px">
<asp:Label ID="salary" Text='<%#Eval("basic") %>' runat="server"></asp:Label>
</td>
<td style="width:100px">
<asp:Label ID="designation" Text='<%#Eval("job") %>' runat="server"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>

Code View:

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;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=hightechnology;Initial Catalog=session;Integrated Security=true");
    protected void Page_Load(object sender, EventArgs e)
    {
        Binddata();
    }
    public void Binddata()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from emp", con);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
        con.Close();
    }
}