In this example, we will use the following XML file ("cdcatalog. xml "):
<? XML version = "1.0" encoding = "ISO-8859-1"?>
<Catalog>
<Cd>
<Title> empire burlesque </title>
<Artist> Bob Dylan </artist>
<Country> USA </country>
<Company> Columbia </company>
<Price> 10.90 </price>
<Year> 1985 </year>
</Cd>
<Cd>
<Title> hide your heart </title>
<Artist> Bonnie Taylor </artist>
<Country> UK </country>
<Company> CBS Records </company>
<Price> 9.90 </price>
<Year> 1988 </year>
</Cd>
<Cd>
<Title> Greatest Hits </title>
<Artist> Dolly Parton </artist>
<Country> USA </country>
<Company> RCA </company>
<Price> 9.90 </price>
<Year> 1982 </year>
</Cd>
<Cd>
<Title> still got the blues </title>
<Artist> Gary Moore </artist>
<Country> UK </country>
<Company> Virgin Records </company>
<Price> 10.20 </price>
<Year> 1990 </year>
</Cd>
<Cd>
<Title> Eros </title>
<Artist> Eros ramazzotti </artist>
<Country> Eu </country>
<Company> BMG </company>
<Price> 9.90 </price>
<Year> 1997 </year>
</Cd>
</CATALOG>
The datalist control, similar to the Repeater control, is used to display duplicate lists of projects restricted to this control. However, the datalist control adds tables to the Data project by default.
<% @ 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")%>"
<%#Container.DataItem("artist")%>-$<%#Container.DataItem("price")%>
</Itemtemplate>
<Footertemplate>
Copy right w3school.com.cn
</Footertemplate>
</ASP: datalist>
</Form>
</Body>
</Html>
Datalist control with styles
<% @ 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")%>"
<%#Container.DataItem("artist")%>-$<%#Container.DataItem("price")%>
</Itemtemplate>
<Footertemplate>
Copy right w3school.com.cn
</Footertemplate>
</ASP: datalist>
</Form>
</Body>
</Html>
Datalist control with <alternatingitemtemplate>
<% @ 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"
Alternatingitemstyle-backcolor = "# e8e8e8"
Alternatingitemstyle-forecolor = "#000000"
Footerstyle-font-size = "9pt"
Footerstyle-font-italic = "true">
<Headertemplate>
My CD catalog
</Headertemplate>
<Itemtemplate>
"<%#Container.DataItem("title")%>"
<%#Container.DataItem("artist")%>-$<%#Container.DataItem("price")%>
</Itemtemplate>
<Alternatingitemtemplate>
"<%#Container.DataItem("title")%>"
<%#Container.DataItem("artist")%>-$<%#Container.DataItem("price")%>
</Alternatingitemtemplate>
<Footertemplate>
Copy right w3school.com.cn
</Footertemplate>
</ASP: datalist>
</Form>
</Body>
</Html>