This documentation is archived and is not being maintained.

DataList Web Server Control Declarative Syntax

Displays the items from a data source by using templates. You can customize the appearance and contents of the control by manipulating the templates that make up the different components of the DataList control, such as the ItemTemplate and HeaderTemplate.

    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    ForeColor="color name|#dddddd"
    OnCancelCommand="CancelCommand event handler"
    OnDataBinding="DataBinding event handler"
    OnDeleteCommand="DeleteCommand event handler"
    OnDisposed="Disposed event handler"
    OnEditCommand="EditCommand event handler"
    OnInit="Init event handler"
    OnItemCommand="ItemCommand event handler"
    OnItemCreated="ItemCreated event handler"
    OnItemDataBound="ItemDataBound event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelectedIndexChanged="SelectedIndexChanged event handler"
    OnUnload="Unload event handler"
    OnUpdateCommand="UpdateCommand event handler"
        <AlternatingItemStyle />
            <!-- child controls -->
        <EditItemStyle />
            <!-- child controls -->
        <FooterStyle />
            <!-- child controls -->
        <HeaderStyle />
            <!-- child controls -->
        <ItemStyle />
            <!-- child controls -->
        <SelectedItemStyle />
            <!-- child controls -->
        <SeparatorStyle />
            <!-- child controls -->

You can manipulate the control's layout and content by defining templates. The following table lists the different templates for the DataList control.


Similar to the ItemTemplate element, but rendered for every other row (alternating rows) in the DataList control. You can specify a different appearance for the AlternatingItemTemplate element by setting its style properties.


The layout of an item when it has been set to edit mode. This template typically contains editing controls (such as TextBox controls). The EditItemTemplate is invoked for a row in the DataList control when the EditItemIndex is set to the ordinal number of that row.


The text and controls to render at the bottom (footer) of the DataList control.

The FooterTemplate cannot be data-bound.


The text and controls to render at the top (header) of the DataList control.

The HeaderTemplate cannot be data-bound.


The elements to render once for each row in the data source.


The elements to render when the user selects an item in the DataList control. Typical uses are to expand the number of data fields displayed and to visually mark the row with a highlight.


The elements to render between each item.

The SeparatorTemplate item cannot be data-bound.

You can customize the appearance of the DataList control by specifying a style for the different parts of the control. The following table lists the style properties that control the appearance of the different parts of the DataList control.

Style property Description Style class


The style for every other item (alternating item).



The style for the item being edited.



The style for the footer at the end of the list (if any).



The style for the header at the beginning of the list (if any).



The style for individual items.



The style for the selected item.



The style for the separator between each item.



The DataList control differs from the Repeater control by supporting directional rendering (by use of the RepeatColumns and RepeatDirection properties) and the option to render within an HTML table.

The Items collection contains the data-bound members of the DataList control. The collection is populated when the DataBind method is called on the DataList control. The header (if any) is added first, then one Item object for each data row. If a SeparatorTemplate exists, Separators are created and added between each item, but these are not added to the Items collection.

After all items have been created for the rows in the DataSource, the Footer is added to the control (but not to the Items collection). Finally, the control raises the ItemCreated event for each item, including the header, footer, and separators. Unlike most collections, the Items collection does not expose Add or Remove methods. However, you can modify the contents within an item by providing a handler for the ItemCreated event.

Caution noteCaution

Text is not HTML encoded before it is displayed in the DataList control. This makes it possible to embed script within HTML tags in the text. If the values for the control come from user input, be sure to validate the values to help prevent security vulnerabilities.

For detailed information on the DataList Web server control's properties and events, see the DataList class documentation.

The following example demonstrates how to use a DataList control to display the items of a data source.

<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
   <script runat="server">
      ICollection CreateDataSource() 
         // Create sample data for the DataList control.
         DataTable dt = new DataTable();
         DataRow dr;
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
         dt.Columns.Add(new DataColumn("StringValue", typeof(String)));
         dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
         dt.Columns.Add(new DataColumn("ImageValue", typeof(String)));
         // Populate the table with sample values.
         for (int i = 0; i < 9; i++) 
            dr = dt.NewRow();
            dr[0] = i;
            dr[1] = "Description for item " + i.ToString();
            dr[2] = 1.23 * (i + 1);
            dr[3] = "Image" + i.ToString() + ".jpg";
         DataView dv = new DataView(dt);
         return dv;
      void Page_Load(Object sender, EventArgs e) 

         // Load sample data only once, when the page is first loaded.
         if (!IsPostBack) 
            ItemsList.DataSource = CreateDataSource();

<head runat="server">
    <title>DataList Example</title>
   <form id="form1" runat="server">

      <h3>DataList Example</h3>
      <asp:DataList id="ItemsList"

         <HeaderStyle BackColor="#aaaadd">

         <AlternatingItemStyle BackColor="Gainsboro">


            List of items


            Description: <br />
            <%# DataBinder.Eval(Container.DataItem, "StringValue") %>

            <br />

            Price: <%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>

            <br />

            <asp:Image id="ProductImage" AlternateText="Product picture" 
                 ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'