Getting Started
  Introduction
  What is ASP.NET?
  Language Support

ASP.NET Web Forms
  Introducing Web Forms
  Working with Server Controls
  Applying Styles to Controls
  Server Control Form Validation
  Web Forms User Controls
  Data Binding Server Controls
  Server-Side Data Access
  Data Access and Customization
  Working with Business Objects
  Authoring Custom Controls
  Web Forms Controls Reference
  Web Forms Syntax Reference

ASP.NET Web Services
  Introducing Web Services
  Writing a Simple Web Service
  Web Service Type Marshalling
  Using Data in Web Services
  Using Objects and Intrinsics
  The WebService Behavior
  HTML Pattern Matching

ASP.NET Web Applications
  Application Overview
  Using the Global.asax File
  Managing Application State
  HttpHandlers and Factories

Cache Services
  Caching Overview
  Page Output Caching
  Page Fragment Caching
  Page Data Caching

Configuration
  Configuration Overview
  Configuration File Format
  Retrieving Configuration

Deployment
  Deploying Applications
  Using the Process Model
  Handling Errors

Security
  Security Overview
  Authentication & Authorization
  Windows-based Authentication
  Forms-based Authentication
  Authorizing Users and Roles
  User Account Impersonation
  Security and WebServices

Localization
  Internationalization Overview
  Setting Culture and Encoding
  Localizing ASP.NET Applications
  Working with Resource Files

Tracing
  Tracing Overview
  Trace Logging to Page Output
  Application-level Trace Logging

Debugging
  The SDK Debugger

Performance
  Performance Overview
  Performance Tuning Tips
  Measuring Performance

ASP to ASP.NET Migration
  Migration Overview
  Syntax and Semantics
  Language Compatibility
  COM Interoperability
  Transactions

Sample Applications
  A Personalized Portal
  An E-Commerce Storefront
  A Class Browser Application
  IBuySpy.com

  Get URL for this page

DataList



Working With DataList

The DataList control displays data items in a repeating list, and optionally supports selecting and editing the items. The content and layout of list items in DataList is defined using templates. At a minimum, every DataList must define an ItemTemplate; however, several optional templates can be used to customize the appearance of the list. The following table describes those templates.

Template Name Description
ItemTemplate Defines the content and layout of items within the list. Required.
AlternatingItemTemplate If defined, determines the content and layout of alternating items. If not defined, ItemTemplate is used.
SeparatorTemplate If defined, is rendered between items (and alternating items). If not defined, a separator is not rendered.
SelectedItemTemplate If defined, determines the content and layout of the selected item. If not defined, ItemTemplate (AlternatingItemTemplate) is used.
EditItemTemplate If defined, determines the content and layout of the item being edited. If not defined, ItemTemplate (AlternatingItemTemplate, SelectedItemTemplate) is used.
HeaderTemplate If defined, determines the content and layout of the list header. If not defined, the header is not rendered.
FooterTemplate If defined, determines the content and layout of the list footer. If not defined, the footer is not rendered.

Templates define the HTML elements and controls that should be displayed for an item, as well as the layout of these elements. Style formatting -- font, color, and border attributes -- is set via styles. Each template has its own style property. For example, styles for the EditItemTemplate are set through the EditItemStyle property.

A third set of properties affect the overall rendering of DataList. By default, DataList items render within a table as a single vertical column. Setting the RepeatLayout property to Flow removes the HTML table structure from the rendering of the list.

DataList supports directional rendering through the RepeatDirection property, meaning it can render its items horizontally or vertically. Since page width is the dimension that the developer must control in Web user interface, DataList permits the developer to control the number of "columns" that are rendered (RepeatColumns), regardless of whether the items are rendered horizontally or vertically.

If RepeatDirection is Horizontal and RepeatColumns is five, the items are rendered in rows containing five columns.

1 2 3 4 5
6 7 8 9 10
11 12 13    

If RepeatDirection is Vertical, and RepeatColumns remains set to five, the items are rendered in five columns, each equal in length to the total number of items divided by five.

1 4 7 10 13
2 5 8 11  
3 6 9 12  

The following sample illustrates using a simple DataList control.

 
VB DataList1.aspx

[Run Sample] | [View Source]

Selecting Items in DataList

You can customize the content and appearance of the selected item through the SelectedItemTemplate property. The SelectedItemTemplate is controlled by the SelectedIndex property. By default the value of SelectedIndex is -1, meaning none of the items in the list is selected. When SelectedIndex is set to a particular item, that item is displayed using the SelectedItemTemplate.

The following sample illustrates using a SelectedItemTemplate in DataList.

 
VB DataList2.aspx

[Run Sample] | [View Source]

Editing DataList Items

The DataList control supports in-place editing of the data in an item through its EditItemTemplate property. The EditItemTemplate defines the content and appearance of the item when it is being edited. For example, the following template includes a text box, an "Update" button and a "Cancel" button.

The EditItemTemplate interacts with another property: EditItemIndex. By default, the value of EditItemIndex is -1, meaning none of the items in the list is being edited. When EditItemIndex is set to a particular item, that item is displayed using the EditItemTemplate.

The DataList also supplies three events that can be used to support editing. EditCommand is thrown when an "edit" command button control is clicked within the list's ItemTemplate. It's up to you to handle this event in your code. The typical logic sets EditItemIndex to the selected item, and then rebinds the data to the DataList as shown in the following example.


Protected Sub DataList_EditCommand(Source As Object, e As DataListCommandEventArgs)
    DataList1.EditItemIndex = CType(e.Item.ItemIndex, Integer)
    BindList()
End Sub
VB

The EditItemTemplate typically contains "update" and "cancel" command buttons. These buttons cause the UpdateCommand and CancelCommand events to be thrown, respectively. It's up to you to handle these events in your code. The typical logic for "cancel" sets EditItemIndex to -1, and then rebinds the data to the DataList as shown in the following example.


Protected Sub DataList_CancelCommand(Source As Object, e As DataListCommandEventArgs)
    DataList1.EditItemIndex = -1
    BindList()
End Sub
VB

The typical logic for "update" updates the data source, sets EditItemIndex to -1, and then rebinds the data to the DataList. The following sample illustrates editing items in DataList.

 
VB DataList3.aspx

[Run Sample] | [View Source]


Copyright 2001 Microsoft Corporation. All rights reserved.