Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

ASP.NET DataList Control

ASP.NET Web Forms - DataList Control


The DataList control, similar to the Repeater control, is used to display a repeated list of items bound to the control. However, the DataList control adds a table to the data items by default.


Binding a DataSet to the DataList Control

The DataList control, similar to the Repeater control, is used to display a repeated 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, an XML file, or other item lists. Here, we will demonstrate how to bind an XML file to the DataList control.

In our example, we will use the following XML file ("cdcatalog.xml"):

Empire Burlesque

Bob Dylan

USA

Columbia

10.90

1985

Hide your heart

Bonnie Tyler

UK

CBS Records

9.90

1988

Greatest Hits

Dolly Parton

USA

RCA

9.90

1982

Still got the blues

Gary Moore

UK

Virgin records

10.20

1990

Eros

Eros Ramazzotti

EU

BMG

9.90

1997

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 the XML file, and load the XML file into the DataSet when the page is first loaded:

gridlines="both" runat="server">

My CD Catalog

"<%#Container.DataItem("title")%>" of

<%#Container.DataItem("artist")%> -

$<%#Container.DataItem("price")%>

Copyright Hege Refsnes

Demo Example »


Using Styles

You can also add styles to the DataList control to make the output fancier:

Example

<%@ Import Namespace="System.Data" %>

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">

My CD Catalog

"<%#Container.DataItem("title")%>" of

<%#Container.DataItem("artist")%> -

$<%#Container.DataItem("price")%>

Copyright Hege Refsnes

Demo Example »


Use

You can add the element after the element to describe the appearance of alternating rows in the output. You can add styles to the data inside the region:

Example

<%@ Import Namespace="System.Data" %>

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">

My CD Catalog

"<%#Container.DataItem("title")%>" of

<%#Container.DataItem("artist")%> -

$<%#Container.DataItem("price")%>

"<%#Container.DataItem("title")%>" of

<%#Container.DataItem("artist")%> -

$<%#Container.DataItem("price")%>

© Hege Refsnes

Demo Example »