Databinding with Repeater control in asp.net with C#


Data presentation is an important goals of any application development process. ASP.NET 2.0 provides many server controls which render data in different rich formats and styles. for example Datalist, Gridview, repeater control etc. Here I am going to discuss about repeater control in detail.

Repeater control is a template based container control. you define layout for the Repeater control by creating different templates based on your needs. The Repeater control may be bound to a database table, an XML file, or another list of items. Here we will show how to bind data to a Repeater control.

Repeater Control Templates

Repeater controls provides different kinds of templates which helps in determining the layout of control's content. Templates generate markup which determine final layout of content. These are as follows:

  • HeaderTemplate
  • ItemTemplate
  • AlternatingItemTemplate
  • FooterTemplate
  • SeparatorTemplate

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

AlternatingItemTemplate: AlternatingItemTemplates define the markup for each Item but for AlternatingItems in DataSource collection like different background color and styles.

HeaderTemplate: HeaderTemplate will emit markup for Header element for DataSource collection

FooterTemplate: FooterTemplate will emit markup for footer element for DataSource collection

SeparatorTemplate: SeparatorTemplate will determine separator element which separates each Item in Item collection. Usually, SeparateTemplate will be <br> html element or <hr> html element.

DataBinding in Repeater Control:

Like any other Data Bound control, Repeater control supports DataSource property which allows you to bind any valid DataSource, any datasets or XML files.

There are the following easy steps to bind repeater control.

Step 1: Create new web application.

Step 2: Drag repeater control from toolbox and drop on the page.

Step 3: add the varies templates like as follows:

InlineCode:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Untitled Page</title>
</
head>
<
body>
<
form id="form1" runat="server">
<
div>
<
asp:Repeater ID="Repeater1" runat="server">
<
HeaderTemplate>
<
table border="0" width="600px" cellpadding="2" cellspacing="1" style="border: 1px solid maroon;">
<
tr bgcolor="maroon">
<
th>
Name</th>
<
th>
Description</th>
<
th>
Email</th>
<
th>
Country</th>
</
tr>
</
HeaderTemplate>
<
ItemTemplate>
<
tr>
<
td width="100">
<%# DataBinder.Eval(Container, "DataItem.Name")%>
</td>
<
td>
<%# DataBinder.Eval(Container, "DataItem.Description")%>
</td>
<
td width="150">
<%# DataBinder.Eval(Container, "DataItem.Email")%>
</td>
<
td width="100" align=center>
<%# DataBinder.Eval(Container, "DataItem.Country")%>
</td>
</
tr>
</
ItemTemplate>
<
AlternatingItemTemplate>
<
tr bgcolor="#e8e8e8">
<
td width="100">
<%# DataBinder.Eval(Container, "DataItem.Name")%>
</td>
<
td>
<%# DataBinder.Eval(Container, "DataItem.Description")%>
</td>
<
td width="150">
<%# DataBinder.Eval(Container, "DataItem.Email")%>
</td>
<
td width="100" align=center>
<%# DataBinder.Eval(Container, "DataItem.Country")%>
</td>
</
tr>
</
AlternatingItemTemplate>
<
FooterTemplate>
</
table>
</
FooterTemplate>
</
asp:Repeater>
<
div style="font-size:14px; color:Navy">Total Items:
<
asp:Label ID=totalcount runat=server></asp:Label>
</
div>
</
div>
</
form>
</
body>
</
html
>

Cpde Behind code:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using
System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
     {
          SqlCommand cmd = new SqlCommand("SELECT * FROM Users", new SqlConnection(@"Server=Puru\SQLSERVER2005;Database=Test; Uid=sa;Pwd=wintellect;"));

          cmd.Connection.Open();
          Repeater1.DataSource = cmd.ExecuteReader();
          Repeater1.DataBind();
          if (Repeater1.Items.Count > 0)
          {
               totalcount.Text = Repeater1.Items.Count.ToString();
          }
          cmd.Connection.Close();
          cmd.Connection.Dispose();
     }
}

Output:

repeater1.JPG

Up Next
    Ebook Download
    View all
    Learn
    View all