Easy Tutorial
❮ Control Textbox Prop Webcontrol Listcontrol Datatextfield ❯

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 around 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 around the data items by default. The DataList control can be bound to database tables, XML files, or other lists of items. 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"):

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 your .aspx page:

Next, create a DataSet for the XML file and load this XML file into the DataSet when the page is first loaded:

Then, 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, and finally, the content in the <FooterTemplate> element appears only once in the output:

Then we add the script to create the DataSet and bind the mycdcatalog DataSet to the DataList control. We then populate the DataList control using a <HeaderTemplate> containing the header, an <ItemTemplate> containing the data items to be displayed, and a <FooterTemplate> containing the text. Note that you can set the gridlines property of the DataList to "both" to display table borders:

Example


Using Styles

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

Example


Using <AlternatingItemTemplate>

You can add an <AlternatingItemTemplate> element after the <ItemTemplate> element to describe the appearance of alternating rows in the output. You can add styles to the data in the <AlternatingItemTemplate> section within the DataList control:

Example

❮ Control Textbox Prop Webcontrol Listcontrol Datatextfield ❯