Display data with Repeater control in ASP.NET
3 Comments |
5464 Views | Post by: Admin
Asp.Net Repeater, Repeater Server Control, Display Data using Repeater
The Repeater control is a ASP.NET Data control that allows you to create custom lists out of any data that is available to the page. In Repeater control doesn’t have built in rendering of its own so w
The Repeater control is a ASP.NET Data control that allows you to create custom lists out of any data that is available to the page. In Repeater control doesn’t have built in rendering of its own so we have to provide the layout for the Repeater control. Repeater control uses following Templates,
Repeater has 5 inline template to format it:
HeaderTemplate: This template is used for elements that you want to render once before your ItemTemplate section.
FooterTemplate: This template is used for elements that you want to render once after your ItemTemplate section.
ItemTemplate: This template is used for elements that are rendered once per row of data. It is used to display records
AlternatingItemTemplate: This template is used for elements that are rendered every second row of data. This allows you to alternate background colors. It works on even number of records only.
SeperatorTemplate: It is used for elements to render between each row, such as line breaks.
Advantages of Repeater Control over GridView in ASP.Net:-
In simple words we can say performance of repeater is far better than gridview. If you need basic rendering for read only items then its better to use repeater and if you need events , pagination and editable controls then you should go for gridview.
Simpler controls with less in built functionality are speedy. you can do implement all functionality of grid view to repeater but you have to do it manually. So it depends upon you requirements either you need repeater or gridview. For example find the source code below:-
Bind Repeater Control:
It’s quite easy to bind Repeater Control. When the page runs, the Repeater control loops through the records in the data source and renders an item for each record.
Note: for sample application, I have used the "NorthWind" Database. You c an download from the Microsoft website.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Repeater control example
public partial class _Default : System.Web.UI.Page
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
protected void BindData()
DataSet ds = new DataSet();
string cmdstr = "Select * from Employees";
SqlCommand cmd = new SqlCommand(cmdstr, conn);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
Repeater1.DataSource = ds;
For more details about repeater control refer to http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater(v=vs.110).aspx
Download Source Codes