Export (0) Print
Expand All

DataList Class

A data bound list control that displays items using templates.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

[ControlValuePropertyAttribute("SelectedValue")] 
public class DataList : BaseDataList, INamingContainer, IRepeatInfoUser
/** @attribute ControlValuePropertyAttribute("SelectedValue") */ 
public class DataList extends BaseDataList implements INamingContainer, IRepeatInfoUser
ControlValuePropertyAttribute("SelectedValue") 
public class DataList extends BaseDataList implements INamingContainer, IRepeatInfoUser

Use the DataList control to display a template-defined data bound list. The DataList control supports selecting and editing.

The contents of the DataList control can be manipulated by using templates. The following table lists the supported templates.

Template Name

Description

AlternatingItemTemplate

If defined, provides the content and layout for alternating items in the DataList. If not defined, ItemTemplate is used.

EditItemTemplate

If defined, provides the content and layout for the item currently being edited in the DataList. If not defined, ItemTemplate is used.

FooterTemplate

If defined, provides the content and layout for the footer section of the DataList. If not defined, a footer section will not be displayed.

HeaderTemplate

If defined, provides the content and layout for the header section of the DataList. If not defined, a header section will not be displayed.

ItemTemplate

Required template that provides the content and layout for items in the DataList.

SelectedItemTemplate

If defined, provides the content and layout for the currently selected item in the DataList. If not defined, ItemTemplate is used.

SeparatorTemplate

If defined, provides the content and layout for the separator between items in the DataList. If not defined, a separator will not be displayed.

At the very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Additional templates can be used to provide a custom look to the DataList control.

The appearance of the DataList control may be customized by setting the style properties for the different parts of the control. The following table lists the different style properties.

Style Property

Description

AlternatingItemStyle

Specifies the style for alternating items in the DataList control.

EditItemStyle

Specifies the style for the item being edited in the DataList control.

FooterStyle

Specifies the style for the footer in the DataList control.

HeaderStyle

Specifies the style for the header in the DataList control.

ItemStyle

Specifies the style for the items in the DataList control.

SelectedItemStyle

Specifies the style for the selected item in the DataList control.

SeparatorStyle

Specifies the style for the separator between the items in the DataList control.

You can also show or hide different parts of the control. The following table lists the properties that control which parts are shown or hidden.

Property

Description

ShowFooter

Shows or hides the footer section of the DataList control.

ShowHeader

Shows or hides the header section of the DataList control.

The display direction of a DataList control can be vertical or horizontal. Set the RepeatDirection property to specify the display direction.

The layout of the DataList control is controlled with the RepeatLayout property. Setting this property to RepeatLayout.Table will display the DataList in a table format, while RepeatLayout.Flow displays the DataList without a table structure.

Caution noteCaution

This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. You can use validation controls to verify user input before displaying the input text in a control. ASP.NET provides an input request validation feature to block script and HTML in user input. For more information, see Securing Standard ControlsHow to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings, and Validating User Input in ASP.NET Web Pages.

TopicLocation
How to: Customize Items Dynamically in the DataList Web Server ControlBuilding ASP .NET Web Applications
How to: Add DataList Controls to an ASP.NET Web PageBuilding ASP .NET Web Applications
How to: Allow Users to Delete Items in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Create ASP.NET Web Control Templates DeclarativelyBuilding ASP .NET Web Applications
How to: Specify Horizontal or Vertical Layout in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Add Controls to an ASP.NET Web Page ProgrammaticallyBuilding ASP .NET Web Applications
How To: Create ASP.NET Web Server Control Templates DynamicallyBuilding ASP .NET Web Applications
How to: Allow Users to Edit Items in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Bind to Data in a Templated ControlBuilding ASP .NET Web Applications
How to: Allow Users to Select Items in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Customize DataList Items at Run TimeBuilding ASP .NET Web Applications
How to: Respond to Button Events in Data-Bound ControlsBuilding ASP .NET Web Applications
How to: Specify Flow or Table Format in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Respond to Button Events in DataList, Repeater, or GridView ItemsBuilding ASP .NET Web Applications
How to: Customize Items Dynamically in the DataList Web Server ControlBuilding ASP .NET Web Applications
How to: Add DataList Controls to an ASP.NET Web PageBuilding ASP .NET Web Applications
How to: Create ASP.NET Web Server Control TemplatesBuilding ASP .NET Web Applications
How to: Allow Users to Delete Items in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Create ASP.NET Web Control Templates DeclarativelyBuilding ASP .NET Web Applications
How to: Specify Horizontal or Vertical Layout in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Add Controls to an ASP.NET Web Page ProgrammaticallyBuilding ASP .NET Web Applications
How To: Create ASP.NET Web Server Control Templates DynamicallyBuilding ASP .NET Web Applications
How to: Allow Users to Edit Items in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Bind to Data in a Templated ControlBuilding ASP .NET Web Applications
How to: Allow Users to Select Items in DataList Web Server ControlsBuilding ASP .NET Web Applications
How to: Customize DataList Items at Run TimeBuilding ASP .NET Web Applications
How to: Respond to Button Events in Data-Bound ControlsBuilding ASP .NET Web Applications
How to: Specify Flow or Table Format in DataList Web Server ControlsBuilding ASP .NET Web Applications
Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web DeveloperBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Displaying and Formatting Data with the DataList Web Server ControlBuilding ASP .NET Web Applications in Visual Studio
How to: Create Web Server Control Templates Using the DesignerBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating a Web Page to Display XML DataBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Data Binding to a Custom Business ObjectBuilding ASP .NET Web Applications in Visual Studio
How to: Add DataList Web Server Controls to an ASP.NET Web Page (Visual Studio)Building ASP .NET Web Applications in Visual Studio
How to: Bind to Data in a Templated Control in Visual StudioBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Creating Master/Detail Web Pages in Visual StudioBuilding ASP .NET Web Applications in Visual Studio
Walkthrough: Displaying an XML Document in a Web Forms Page Using TransformationsBuilding ASP .NET Web Applications in Visual Studio
How to: Respond to Button Events in DataList, Repeater, or GridView ItemsBuilding ASP .NET Web Applications

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

NoteNote

The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.


<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
 
<html>
   <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";
 
            dt.Rows.Add(dr);
         }
 
         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();
            ItemsList.DataBind();
         }

      }
 
   </script>
 
<body>
 
   <form runat=server>

      <h3>DataList Example</h3>
 
      <asp:DataList id="ItemsList"
           BorderColor="black"
           CellPadding="5"
           CellSpacing="5"
           RepeatDirection="Vertical"
           RepeatLayout="Table"
           RepeatColumns="3"
           ShowBorder="True"
           runat="server">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>

         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <HeaderTemplate>

            List of items

         </HeaderTemplate>
               
         <ItemTemplate>

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

            <br>

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

            <br>

            <asp:Image id="ProductImage"
                 ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
                 runat="server"/>

         </ItemTemplate>
 
      </asp:DataList>
 
   </form>
 
</body>
</html>


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft