In this article I’m
going to explain how to bind DataList from XML file in ASP.NET using C#.
What is DataList .?
The DataList Web server control displays data in a
format that you can define using templates and styles. The DataList control is useful for data in any
repeating structure, such as a table. The DataList control can display rows in different
layouts, such as ordering them in columns or rows.
Template property and
Description:
ItemTemplate: Contains the HTML elements and
controls to render once for each row in the data source.
AlternatingItemTemplate: Contains the HTML elements and
controls to render once for every other row in the data source. Typically, you
use this template to create a different look for the alternating rows, such as
a different background color than the color that is specified in the ItemTemplate property.
SelectedItemTemplate: Contains the elements to render
when the user selects an item in the DataList control. Typically, you use this
template to visually distinguish the selected row with a different background
or font color. You can also expand the item by displaying additional fields
from the data source.
EditItemTemplate: Specifies the layout of an item
when it is in edit mode. This template typically contains editing controls,
such as TextBoxcontrols.
HeaderTemplate and FooterTemplate: Contains the text and controls
to render at the beginning and end of the list, respectively.
SeparatorTemplate: Contains the elements to render
between each item. A typical example might be a line (using an HR element).
Here I’ll show you how to bind
DataList from XML file to display employee details by repeating structure such
as table.
Here we have to create XML file and
write some sample data about employee details.
<?xml version="1.0" encoding="utf-8" ?>
<EmployeeDetails>
<Employee>
<empid>1001</empid>
<name>Sachin</name>
<designation>Software Engineer</designation>
<city>Chennai</city>
<country>India</country>
</Employee>
<Employee>
<empid>1002</empid>
<name>Suresh</name>
<designation>Web Developer</designation>
<city>New Delhi</city>
<country>India</country>
</Employee>
<Employee>
<empid>1003</empid>
<name>Steve</name>
<designation>Web Developer</designation>
<city>Bangalore</city>
<country>India</country>
</Employee>
<Employee>
<empid>1004</empid>
<name>Karthik</name>
<designation>Business Analyst</designation>
<city>New Delhi</city>
<country>India</country>
</Employee>
</EmployeeDetails>
Designer source code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" BackColor="Gray" BorderColor="#666666"
BorderStyle="None"
BorderWidth="2px"
CellPadding="3"
CellSpacing="2"
Font-Names="Verdana"
Font-Size="Small"
GridLines="Both"
RepeatColumns="3"
RepeatDirection="Horizontal"
Width="600px">
<FooterStyle BackColor="#F7DFB5"
ForeColor="#8C4510"
/>
<HeaderStyle BackColor="#333333"
Font-Bold="True"
Font-Size="Large"
ForeColor="White"
HorizontalAlign="Center"
VerticalAlign="Middle"
/>
<HeaderTemplate>
Employee
Details</HeaderTemplate>
<ItemStyle BackColor="White"
ForeColor="Black"
BorderWidth="2px"
/>
<ItemTemplate>
<b>Employee ID:</b>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("empid") %>'></asp:Label>
<br />
<b>Employee Name:</b>
<asp:Label ID="lblCName"
runat="server"
Text='<%# Bind("name") %>'></asp:Label>
<br />
<b>Designation:</b>
<asp:Label ID="lblName"
runat="server"
Text='<%# Bind("designation") %>'></asp:Label>
<br />
<b> City:</b>
<asp:Label ID="lblCity"
runat="server"
Text=' <%# Bind("city") %>'></asp:Label>
<br />
<b>Country:</b>
<asp:Label ID="lblCountry"
runat="server"
Text='<%# Bind("country") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
Code behind:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection
conn = new SqlConnection("Data Source=SPIDER;Initial
Catalog=Northwind;Integrated Security=True");
protected
void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected
void BindData()
{
DataSet ds = new DataSet();
try
{
ds.ReadXml(Server.MapPath("EmployeeDetails.xml"));
if (ds != null
&& ds.HasChanges())
{
DataList1.DataSource = ds;
DataList1.DataBind();
}
else
{
DataList1.DataBind();
}
}
catch (Exception ex)
{
}
}
}