ASP.NET Web Forms - DataList control
This section introduces The ASP.NET DataList control is more flexible than the Repeater control. The DataList control presents data in the form of a table.The DataList control, similar to the Repeater control, is used to display a repeating list of items bound to the control. However, the DataList control adds a table to the data items by default.
Bind DataSet to DataList control
The DataList control, similar to the Repeater control, is used to display a repeating list of items bound to the control. However, the DataList control adds a table to the data items by default. The DataList control can be bound to a database table, XML file, or other list of items. Here, we will demonstrate how to bind an XML file to a DataList control.
In our 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 Tyler</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>View this XML file: cdcatalog.xml
First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of your .aspx page:
<%@ Import Namespace="System.Data" %>Next, create a DataSet for the XML file and load the XML file into the DataSet when the page first loads:
<script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))end ifend subThen we create a DataList control in the .aspx page. The content in the <HeaderTemplate> element is rendered first and appears only once in the output, while the content in the <ItemTemplate> element is repeated for each "record" in the DataSet. Finally, the content in the <FooterTemplate> element is Appears only once in the output:
<html><body><form runat="server"><asp:DataList id="cdcatalog" runat="server"><HeaderTemplate>...</HeaderTemplate><ItemTemplate>...</ItemTemplate>< FooterTemplate>...</FooterTemplate></asp:DataList></form></body></html>Then we add the script to create the DataSet and bind the mycdcatalog DataSet to the DataList control. The DataList control is then populated with a <HeaderTemplate> that contains the header, an <ItemTemplate> that contains the data items to be displayed, and a <FooterTemplate> that contains the text. Note that you can set the DataList's gridlines property to "both" to display table borders:
Example
<%@ Import Namespace="System.Data" %><script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))cdcatalog.DataSource=mycdcatalogcdcatalog. DataBind()end ifend 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>Use styles
You can also add styles to the DataList control to make the output more fancy:
Example
<%@ Import Namespace="System.Data" %><script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))cdcatalog.DataSource=mycdcatalogcdcatalog. DataBind()end ifend sub</script><html><body><form runat="server"><asp:DataList id="cdcatalog"runat="server"cellpadding="2"cellspacing="2"borderstyle="inset"backcolor="#e8e8e8"headerstyle-font-name="Verdana"headerstyle-font-size="12pt"headerstyle -horizonta lalign="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>Use <AlternatingItemTemplate>
You can add an <AlternatingItemTemplate> element after an <ItemTemplate> element to describe the appearance of alternating rows in the output. You can add styles to the data in the <AlternatingItemTemplate> area inside the DataList control:
Example
<%@ Import Namespace="System.Data" %><script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycdcatalog=New DataSetmycdcatalog.ReadXml(MapPath("cdcatalog.xml"))cdcatalog.DataSource=mycdcatalogcdcatalog. DataBind()end ifend sub</script><html><body><form runat="server"><asp:DataList id="cdcatalog"runat="server"cellpadding="2"cellspacing="2"borderstyle="inset"backcolor="#e8e8e8"headerstyle-font-name="Verdana"headerstyle-font-size="12pt"headerstyle -horizontalalign="center"headerstyle-font-bold="Tr ue"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")%>" of<%#Container.DataItem("artist")%> -$<%#Container.DataItem("price" )%></ItemTemplate><AlternatingItemTemplate>"<%#Container.DataItem("title")%>" of<%#Container.DataItem("artist")%> -$<%#Container.DataItem("price")%></AlternatingItemTemplate><FooterTemplate>© Hege Refsnes</FooterTemplate></asp:DataList>< /form></body></html>