The things I learnt while I migrated from classic asp to asp.net is given here for my reference as well as for new beginners.

ASP.NET CODING FOR REPEATER CONTROL

ALL ABOUT

REPEATER CONTROL

ASP.NET VB code for Repeater Control

(program tested ok)

<%@ Page Language="vb" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load

Dim sql As String
Dim cmd As OleDbCommand
Dim rdr As OleDbDataReader
Dim connString As String
connString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & Server.MapPath("emp.mdb")
Dim conn As OleDbConnection = New OleDbConnection(connString)

conn.Open()
sql="SELECT * FROM emp"
cmd=New OledbCommand(sql,conn)
rdr=cmd.ExecuteReader()
repeater1.DataSource=rdr
repeater1.DataBind()
rdr.Close()
conn.Close()
end sub
</script>

<html>
<head></head>
<body>
<form id="Form1" runat="server">
<asp:Repeater id="repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container, "DataItem.empName")%></td>
<td><%#DataBinder.Eval(Container, "DataItem.age")%></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#DataBinder.Eval(Container, "DataItem.empName")%></td>
<td><%#DataBinder.Eval(Container, "DataItem.age")%></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>

C# code (with code-behind-file) for Repeater

(program tested ok)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="_Default" %>
<html>
<head></head>
<body>

<form id="Form1" runat="server">
<asp:Repeater id="repeater1" runat="server">

<HeaderTemplate>
<table>
<tr>
<th>Name</th>
<th>Age</th>

</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container,"DataItem.EmpName")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Age")%></td>
</tr>
</ItemTemplate>

<AlternatingItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#DataBinder.Eval(Container,"DataItem.empName")%></td>
<td><%#DataBinder.Eval(Container,"DataItem.Age")%></td>
</tr>
</AlternatingItemTemplate>


<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>

<br />

Total Records:
<asp:Label ID=totalRecords runat=server></asp:Label>

</form>

</body>
</html>


 

CODE in the Code-behiind-file:Default.aspx.cs


using System;//this line must for Striang Variable
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;//this line is must
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String connString="Provider=Microsoft.Jet.OLEDB.4.0; data source =" + Server.MapPath("emp.mdb");
OleDbConnection conn=new OleDbConnection(connString);
conn.Open();
String sql="SELECT * FROM emp";
OleDbCommand cmd=new OleDbCommand(sql,conn);
OleDbDataReader rdr=cmd.ExecuteReader();
repeater1.DataSource=rdr;
repeater1.DataBind();
if (repeater1.Items.Count > 0)
{
totalRecords.Text = repeater1.Items.Count.ToString();
}
rdr.Close();
conn.Close();
}
}

 


Efficiency of Repeater Control:

  • Repeater control is simple and light weight for report generation.
  • Repeater control evaluates the data during runtime by late binding. So there will be a performance penalty.
  • Any kind of html markup codes can be addred to the repeater. That is why it is known as more flexible.
  • Datagrid is having in-buit Selection/Editing.But Repeater control does not have this facility.


Syntax of Repeater Control

Repeater control supports the following templates

HeaderTemplate
FooterTemplate
ItemTemplate
AlternatingItemTemplate
SeparatorTemplate( Styles for separating rows and columns)

The Syntax: of Repeater Control

<asp:Repeater id="idrepeater" runat="server" Datasource="<%# dataset %>" DataMember="table-inside-dataset">

  • <HeaderTemplate>..Header Format ..</HeaderTemplate>
  • <ItemTemplate> .. Item formats .. </ItemTemplate>
  • <FooterTemplate>..Header Format ..</FooterTemplate>

</asp:Repeater>

Meaning of DataBinder.Eval()

  • The System.Web.UI.DataBinder class provides a STATIC method DataBinder.Eval.
  • It evaluates the data at runtime by using late binding.

Repeater Example:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>

<a href="<%#DataBinder.Eval(Container.DataItem("empID"))%>">
<%#DataBinder.Eval(Container.DataItem("empName"))%>

</a>

</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>

Repeater Example:

<asp:Repeater id="Repeater1" runat="server">
<HeaderTemplate>

</HeaderTemplate>


<ItemTemplate>
<a href="<%#DataBinder.Eval(Container.DataItem("empID"))%>">
<%#DataBinder.Eval(Container.DataItem("empName"))%>
</a> </ItemTemplate>

<AlternatingItemTemplate>

<font style="background-color:#f0f0f0"><a href = "<%#DataBinder.Eval(Container.DataItem("empID"))%>">
<%#DataBinder.Eval(Container.DataItem("empName"))%></a>

</font>

</AlternatingItemTemplate>

<SeparatorTemplate>
*******************************<br />
</SeparatorTemplate>


<FooterTemplate>

<p>Thank you</p>
</FooterTemplate>

</asp:Repeater>

Sample code for Binding a Repeater with SQLServer:


SqlConnection conn = new SqlConnection(connStr);


if (conn.State == ConnectionState.Closed)
{
conn.Open();
}


SqlCommand cmd = new SqlCommand("select * from emp", conn);

cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSetds = new DataSet();

da.Fill(ds);
Repeater1.DataSource = ds;
Repeater1.DataBind();


if (conn.State == ConnectionState.Open)
{
conn.Close();
}

Thanks for Your Visit

Google Search
Disclaimer and Copy Right