Repeater Control in asp.net
In this Post I will show you how to display the data using Repeater Control or data binding features using Repeater.Repeater is a control that allows you to create custom list from any type of data available on the Page. The repeater control may be bound MySQL database file, Oracle database file, html table and another items or files.
For Customizing the behavior of Repeater, you have to use built in Templates. The Repeater controls the 5 templates.
1. Header Template
2. Item Template
3. Separator Template
4. AlternatingItemTemplate
5. Footer Template
Example of Repeater Control
Here we will show how to bind an xml file to a Repeater Control.
To create a new web page start visual studio click File -> New -> Web Site
Add aspx page then drag the Repeater Control into aspx page from Toolbox.
Go to Website Menu click Add New Item -> XML File
Name the xml file CompinicXML (or whatever you want)
We will use the CompinicXML file in our example
Click Add and paste the following code in xml file.
-----------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Parts>
<category>
<DesktopComputer>CPU</DesktopComputer>
<Mobile>Charger</Mobile>
<Television>Cabel</Television>
<Laptop>Charger</Laptop>
</category>
<category>
<DesktopComputer>Mouse</DesktopComputer>
<Mobile>EarPhone</Mobile>
<Television>Set Top Box</Television>
<Laptop>Laptop Bag</Laptop>
</category>
<category>
<DesktopComputer>Monitor</DesktopComputer>
<Mobile>Battery</Mobile>
<Television>Remote</Television>
<Laptop>Hard Disk</Laptop>
</category>
<category>
<DesktopComputer>Keyboard</DesktopComputer>
<Mobile>Sim Card</Mobile>
<Television>T.V. Trolly</Television>
<Laptop>Battery</Laptop>
</category>
</Parts>
Now add Following highlight namespace to your example
using System.Data;
The System.Data namespaces contain classes for accessing and managing data from diverse sources.Now create a DataSet and load the XML file into the DataSet on page load events.
Double click on RepeaterExample.aspx page and paste the following highlight code.
-----------------------------------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/CompinicXML.xml"));
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
-----------------------------------------------------------------HTML source for Repeater Control
Add Highlight code to your RepeaterExample.aspx Page.
-----------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterExample.aspx.cs" Inherits="RepeaterExample" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<th>Desktop Computer</th>
<th>Mobile</th>
<th>Television</th>
<th>Laptop</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#DataBinder.Eval(Container.DataItem,"DesktopComputer")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"Mobile") %></td>
<td><%#DataBinder.Eval(Container.DataItem,"Television")%></td>
<td><%#DataBinder.Eval(Container.DataItem,"Laptop")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
Press F5 or goto Debug -> Start Dubbing your output window should look like this
Desktop Computer
|
Mobile
|
Television
|
Laptop
|
CPU
|
Charger
|
Cabel
|
Charger
|
Mouse
|
EarPhone
|
Set Top Box
|
Laptop Bag
|
Monitor
|
Battery
|
Remote
|
Hard Disk
|
Keyboard
|
Sim Card
|
T.V. Trolly
|
Battery
|
Related Repeater Example
Bind a Dataset to a Repeater Control (ASP.Net)
Retrieve HTML table with database and Repeater
Retrieve images using a file path stored in database (ASP.Net With C#)
Adding Row in asp.net Repeater (ASP.net with C#)
How to update, delete, insert data in repeater?
0 comments:
Post a Comment