To display a repeated list of items that are bound to the control,The DataList control is used.
A D V E R T I S E M E N T
However,by default the DataList control adds a table around the data items.
Bind a DataSet to a DataList Control
To display a repeated list of items that are bound to the control,The DataList control is used. However,by default the DataList control adds a table around the data items. The DataList control may be bound to an XML file,a database table, or another list of items. Here we will show how to bind an XML file to a DataList control.
We will use the following XML file in our examples ("cdcatalog.xml"):
At first, import the "System.Data" namespace.
To work with the DataSet objects,we need this namespace.
At the top of an .aspx page,include the following directive:
<%@ Import Namespace="System.Data" %>
Next, create a DataSet for the XML file and when the page is first loaded,load the XML file into the DataSet:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
end if
end sub
Then in an .aspx page,we create a DataList. The contents of
the <HeaderTemplate> element are rendered first and only once
within the output and then the contents of the <ItemTemplate>
element are repeated for each "record" in the DataSet, and last,
the contents of the <FooterTemplate> element are rendered once
within the output:
Then we add the following script that creates the DataSet and binds
the mycdcatalog DataSet to the DataList control and also fill
the DataList control with a <HeaderTemplate> that contains the
header of the table, an <ItemTemplate> that contains the data
items to display, and a <FooterTemplate> that contains a text.
Note that to display table borders,the gridlines attribute of the DataList is set to
"both":
<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:DataList id="cdcatalog"
gridlines="both" runat="server">
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>
<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
Using Styles
To make the output more fancy,you can also add styles to the DataList control:
<%@ Import Namespace="System.Data" %>
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycdcatalog=New DataSet
mycdcatalog.ReadXml(MapPath("cdcatalog.xml"))
cdcatalog.DataSource=mycdcatalog
cdcatalog.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="true"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="true">
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>
<FooterTemplate>
Copyright Hege Refsnes
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
Using the <AlternatingItemTemplate>
After the <ItemTemplate> element,you can add an <AlternatingItemTemplate> element to describe the appearance of alternating rows of output and you may style the data in the <AlternatingItemTemplate> section within the DataList control: