ASP.net Repeater Control...?


The Repeater control is a data-bound control that uses templates to display data. This artical i'm trying to explain what is repeater control, when we use Repeater control, how to bind datasource to Repeater control and how to display the result set to Repeater control.

ASP.net Repeater Control:



In this artical i'm trying to explain what is repeater control, when we use Repeater control, how to bind datasource to Repeater control and how to display the result set to Repeater control.

Use of Repeater Control:



The Repeater control is a data-bound control that uses templates to display data. Repeater control is used to display the table structureof data same like GridView but the unique feature of REpeater control is faster execution no other controls will execute like a REpeater control. Repeater control is light weight control. Using HTML table structure we can implement the design for Repeater control. See below code snippet for designing.

Source Code:




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

<!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>
<style type="text/css">
.style1
{
font-size: x-large;
color: #003300;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">

<span class="style1"><strong><em>Working with Repeater Control</em></strong></span><br />
<br />
<br />
<asp:Label ID="lblName" runat="server" Text="Dept Name :"></asp:Label>

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblLoc" runat="server" Text="Location :"></asp:Label>

<asp:TextBox ID="txtLoc" runat="server"></asp:TextBox>
<br />
<br />
<br />

<asp:Button ID="btnView" runat="server" onclick="btnView_Click" Text="View" />
<br />
<br />
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table style="background-color:Blue; color: White">
<tr>
<th align="center" width="50%">
Department Name
</th>
<th align="center" width="50%">
Location
</th>
</tr>
</table>
</HeaderTemplate>

<ItemTemplate>
<table>
<tr>
<td align="center" width="50%">
<%#Container.ItemIndex+1 %>) <%#Eval("dname") %>
</td>
<td align="center" width="50%">
<%#Eval("loc") %>
</td>
</tr>
</table>

</ItemTemplate>
</asp:Repeater>

</div>
</form>
</body>
</html>


After Design the Page like above then implement code based on below.

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;

public partial class Repeater : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("DataBase=ENGSOFT;User id=sa;Password=P@ssword9");
SqlDataAdapter da;
DataSet ds;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind_GV();
}
}
protected void btnView_Click(object sender, EventArgs e)
{
Insert_Data();
Clear();
}
protected void Bind_GV()
{
con.Open();
da = new SqlDataAdapter("select deptno,dname,loc from DEPT", con);
ds = new DataSet();
da.Fill(ds);

con.Close();
Repeater1 .DataSource = ds;
Repeater1.DataBind();
}
protected void Insert_Data()
{
con.Open();
da = new SqlDataAdapter("insert into Dept values(@dname,@loc)", con);
cmd = new SqlCommand("insert into Dept values(@dname,@loc)", con);
cmd.Parameters.AddWithValue("@dname", txtName.Text);
cmd.Parameters.AddWithValue("@loc", txtLoc.Text);
cmd.ExecuteNonQuery();
ds = new DataSet();
con.Close();
Bind_GV();
Clear();
}
protected void Clear()
{
txtLoc.Text = "";
txtName.Text = "";
}
}


OutPut:



Display

Insert

Conclusion:



Repeater control works faster execution, no other control will work that much. But the disadvantage is it doesn't have any built-in support for paging, editing, or sorting of the data that is rendered through one or more of its templates.


Attachments

  • Source Code (45071-21105-Source-Code.zip)
  • Article by naveensanagasetti
    I hope you enjoyed to read my article, If you have any queries out of this then please post your comments.

    Follow naveensanagasetti or read 139 articles authored by naveensanagasetti

    Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: