Using the Repeater Control ASP.Net

Repeater Control in ASP .Net:

The Repeater control performs a very common function that most Web developers have encountered in their projects work. Very often you need to display records from a database, and most likely you would use a FOR loop to write the HTML code so that the records can be displayed within a table. Using the Repeater control, this process can very easily be automated.

A Repeater control is a light weight control which can be used for simple reporting purposes. It supports basic event-handling like Init, Load, Unload etc., This also supports some basic formatting of data and can be presented to the user. A Repeater control offers limited level of data editing or selecting capabilities. For such editing and updates ASP .Net offers DataList and DataGrid controls.


A Repeater control can be used to build small and Flexible reports with the templates for the items. It supports the following five templates.


HeaderTemplate : Can be used to create the Header Rows of the Table data being presented by Repeater control.

FooterTemplate : Can be used to create the Footer Rows of the Table data being presented by Repeater control.

ItemTemplate : Used for formatting the Items and rows to be displayed.

AlternatingItemTemplate : Alternating items formatting.

SeparatorTemplate : Styles for separating rows and columns.

There are many ways in which a control in ASP .Net can be formatted. It is up to the imagination of the programmer to come up with nice formats.

When the page runs, the control loops through the records in the data source and renders the specified template for each record. In addition, before and after processing the data items, the Repeater emits some markup for the header and the footer of the resulting structure. Because the Repeater control has no default appearance, it is useful for creating virtually any kind of list with any layout and style.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<SCRIPT runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack) LoadData();
}

public void LoadData()
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Office",
"server=(local);database=MyDB;Integrated Security=SSPI");
DataTable table = new DataTable();
adapter.Fill(table);
Repeater1.DataSource = table;
Repeater1.DataBind();
}
</SCRIPT>

<HTML>
<HEAD>
<title>ListControls #1</title>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Repeater runat="server" id="Repeater1">
<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem, "Empid") %></b>
<br>Join Date: <%# DataBinder.Eval(Container.DataItem,
"joindate", "{0:d}") %>

Transfer Date: <%# DataBinder.Eval(Container.DataItem,
"transferdate", "{0:d}") %>
</itemtemplate>
<separatortemplate>
<hr>
</separatortemplate>
</asp:Repeater>
</form>
</body>
</HTML>
Tags: ,
Hot on Web:


About author