ASP.NET - DataList Control
- Previous Page WebForms Repeater
- Next Page WebForms Database Connection
The DataList control, similar to the Repeater control, is used to display a repeated list of items limited to the control. However, the DataList control will add a table by default to the data items.
Binding DataSet to DataList Control
The DataList control, similar to the Repeater control, is used to display a repeated list of items limited to the control. However, the DataList control will add a table by default to the data items. The DataList control can be bound to a database table, an XML file, or other item lists. Here, we will show how to bind an XML file to a DataList control.
We will use the following XML file ("cdcatalog.xml") in the example:
<?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>
Please view this XML file:cdcatalog.xml
First, import the "System.Data" namespace. We need this namespace to work with the DataSet object. Include the following directive at the top of the .aspx page:
<%@ Import Namespace="System.Data" %>
Next, create a DataSet for this XML file and load this XML file into the DataSet when the page is first loaded:
<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 we create a DataList control in the .aspx page. The content of the <HeaderTemplate> element appears only once in the output, while the content of the <ItemTemplate> element corresponds to the repetition of "record" in the DataSet, and finally, the content of the <FooterTemplate> 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 this mycdcatalog DataSet to the DataList control. We also use these elements to fill the DataList control: the <HeaderTemplate> containing the table header, the <ItemTemplate> containing the data items to be displayed, and the <FooterTemplate> containing text. Note that the gridlines property of the DataList is set to "both" to display the table borders:
<%@ 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 codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
Using styles
You can also add styles to the DataList control to make it more stylish:
<%@ 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 codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
Using <AlternatingItemTemplate>
You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe the appearance of alternating rows. You can also style the data in the <AlternatingItemTemplate> section within 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" 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> © codew3c.com </FooterTemplate> </asp:DataList> </form> </body> </html>
- Previous Page WebForms Repeater
- Next Page WebForms Database Connection