Quantcast
Channel: Microsoft Technologies & Solutions – JHARAPHULA
Viewing all articles
Browse latest Browse all 17

How to bind xml data to ASP.NET repeater control?

$
0
0

To present data from several data source ASP.NET introduced controls like datagrid, datalist & repeater. Compare to datagrid repeater is connection oriented & gives faster access to data. Inner logic of repeater is similar to pointer. During data bind for custom structure repeater control is very useful. In this example I created a demo app to display a xml file data in tabular shape using ASP.NET repeater control.

In the below example I have a employees.xml file which contains name, designation, company & salary of several employees. What I want is, I want to display these data in a HTML Table using ASP.NET repeater control. To achieve this in code-behind file “Default.aspx.vb” page_load event I am creating an instance of new DataSet as employeelog. Then using ReadXml() method I am assigning the xml file data to the DataSet employeelog. Later to bind these data in repeater control I pointed the repeater control DataSource to DataSet employeelog. Finally calling emplog.DataBind() to bind xml data.

Now in Default.aspx page I have my repeater control is with data. To show these data in particular table rows & columns here I used <%#Container.DataItem(“Respective Field Name”)%> inside ItemTemplate of repeater control. To make each data rows user friendly here with ItemTemplate I used AlternatingItemTemplate with background color #e8e8e8.

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>How to bind xml data to ASP.NET repeater control?</title>
</head>
<body>
<form id="frmRepeater" runat="server">
<asp:Repeater id="emplog" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Employee Name</th>
<th>Designation</th>
<th>Company</th>
<th>Monthly Salary</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td><%#Container.DataItem("name")%> </td>
<td><%#Container.DataItem("designation")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("salary")%> </td>
</tr>
</ItemTemplate>

<AlternatingItemTemplate>
<tr bgcolor="#e8e8e8">
<td><%#Container.DataItem("name")%> </td>
<td><%#Container.DataItem("designation")%> </td>
<td><%#Container.DataItem("company")%> </td>
<td><%#Container.DataItem("salary")%> </td>
</tr>
</AlternatingItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>
</body>
</html>

Default.aspx.vb

Imports System.Data

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim employeelog = New DataSet
employeelog.ReadXml(MapPath("employees.xml"))
emplog.DataSource = employeelog
emplog.DataBind()
End If
End Sub
End Class

employees.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<employees>
<emp>
<name>Sujata Panigraphi</name>
<designation>Chief Business Officer</designation>
<company>Infosys</company>
<salary>92,000</salary>
</emp>
<emp>
<name>Abhishek Srivastab</name>
<designation>Technical Head</designation>
<company>Cognizant Solution</company>
<salary>75,000</salary>
</emp>
<emp>
<name>Biswabhusan Panda</name>
<designation>Project Manager</designation>
<company>TATA Technology</company>
<salary>86,000</salary>
</emp>
<emp>
<name>Sunanda Patnayak</name>
<designation>Team Leader</designation>
<company>Wipro</company>
<salary>82,000</salary>
</emp>
<emp>
<name>Raghav Roy</name>
<designation>Sr. UI Developer</designation>
<company>Persistent System</company>
<salary>61,000</salary>
</emp>
</employees>

The post How to bind xml data to ASP.NET repeater control? appeared first on JHARAPHULA.


Viewing all articles
Browse latest Browse all 17

Trending Articles