Server Intellect
 
Home > Articles > ASP.NET Articles > How to display data in asp.net Repeater control?

How to display data in asp.net Repeater control?

by Thamilselvan Jagadeesan   on Jun 26, 2013   Category: ASP.NET  | Level: Intermediate  |  Views: 16199    |  Points: 100   
Like this article? Bookmark and Share:
In this article, I will show you how to display the data using repeater control which is basic and light weight control in asp.net.




Introduction


As we know that displaying data to the user in each and every application is very important and asp.net provides many controls with displaying a data in well formatted manner. Here the repeater control is used to display the repeated list of items which are bound to the control.  This control can bound to data sources such as xml or database.

Repeater control supports datasource property which is used to bind the control to any datasources and it provides the templates to define the layout of the content. Actually this control does not support built in edit, paging and styles. So explicitly we need to do the design layout of the controls data using html tables.

Display data in repeater control


Now create the asp.net project and add aspx page then drag the Repeater control into aspx page, so now the page looks as shown below

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

<!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 id="Head1" runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div style="font-family:Verdana;font-size:medium">

<u>Displaying data in Repeater Control</u>

</div><br />

<div>

<asp:Repeater ID="RepSample" runat="server">

</asp:Repeater>

</div>

</form>

</body>

</html>





 Now we are going to add templates in the repeater control to design the layout and this control will supports below templates

   1.HeaderTemplate will be used to display the header of the repeater and it will render only once before itemTemplate part
   2.FooterTemplate will be used to display the data in footer area and it will render after itemTemplate part.
   3.ItemTemplate will be used to display the data in the control and will render once per each row of data.
   4.AlternatingItemTemplate will be used to format the alternate/other row of data.
   5.SeperatorTemplate will be used to separate the each row by using html <br> or <hr>.

 Data source

Now we are going to prepare the data source to load into repeater control. Here I’m using data with DataTable to bind into repeater control. So first load some data into datatable and bind into repeater control in page load event as shown below.

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

DataTable dtEmpDet = GetData();

RepSample.DataSource = dtEmpDet;

RepSample.DataBind();

}

}

private DataTable GetData()

{

DataTable dt = new DataTable();

dt.Clear();

dt.Columns.Add("Name");

dt.Columns.Add("Designation");

dt.Columns.Add("Location");

DataRow rw1 = dt.NewRow();

rw1["Name"] = "Ranjth";

rw1["Designation"] = "Developer";

rw1["Location"] = "Delhi";

dt.Rows.Add(rw1);

DataRow rw2 = dt.NewRow();

rw2["Name"] = "Srini";

rw2["Designation"] = "Trainee";

rw2["Location"] = "Delhi";

dt.Rows.Add(rw2);

DataRow rw3 = dt.NewRow();

rw3["Name"] = "Kapil";

rw3["Designation"] = "Sr Developer";

rw3["Location"] = "Bombay";

dt.Rows.Add(rw3);

DataRow rw4 = dt.NewRow();

rw4["Name"] = "Sanjai";

rw4["Designation"] = "Tech Lead";

rw4["Location"] = "Delhi";

dt.Rows.Add(rw4);

DataRow rw5 = dt.NewRow();

rw5["Name"] = "Arvind";

rw5["Designation"] = "Project Manager";

rw5["Location"] = "Chennai";

dt.Rows.Add(rw5);

return dt;

}

 

Design aspx page as shown below.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterControl.aspx.cs"

Inherits="SampleRepeater.RepeaterControl" %>

<!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>ASP.NET Repeater Control</title>

</head>

<body>

<form id="form1" runat="server">

<div style="font-family:Verdana;font-size:medium">

<u>Displaying data in Repeater Control</u>

</div>

<br />

<div>

<asp:Repeater ID="RepSample" runat="server">

<HeaderTemplate>

<table cellpadding="1" cellspacing="1" width="100%" style="font-family:Verdana;border:1px solid #C0C0C0;background-color:#D8D8D8">

<tr bgcolor="#FF781E" >

<th>

Name

</th>

<th>

Designation

</th>

<th>

Location

</th>

</tr>

</HeaderTemplate>

<ItemTemplate>

<tr style="background-color:White">

<td>

<%#DataBinder.Eval(Container, "DataItem.Name")%>

</td>

<td>

<%#DataBinder.Eval(Container, "DataItem.Designation")%>

</td>

<td>

<%#DataBinder.Eval(Container,"DataItem.Location")%>

</td>

</tr>

</ItemTemplate>

<AlternatingItemTemplate>

<tr>

<td>

<%#DataBinder.Eval(Container, "DataItem.Name")%>

</td>

<td>

<%#DataBinder.Eval(Container, "DataItem.Designation")%>

</td>

<td>

<%#DataBinder.Eval(Container,"DataItem.Location")%>

</td>

</tr>

</AlternatingItemTemplate>

<FooterTemplate>

</table>

</FooterTemplate>

</asp:Repeater>

</div>

</form>

</body>

</html>

So once you complete the above part and run the project. Repeater control will load the data and display as like below

Above itemTemplate and AlternatingItemTemplate using DataBinder.Eval which evalutes data binding expresaaion at runtime and it will render each row of data in the control.

I have attached the sample project here, download and see how it works. Thanks for reading this article and I hope this article would help how to display data in repeater control in simple steps. Pleae provide your feedback and suggestion.

Like this article? Bookmark and Share:

Most viewed Articles

User Comments


No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback