This documentation is archived and is not being maintained.

ListViewDataItem Class

Represents an individual data item in a ListView control.

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

[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class ListViewDataItem : ListViewItem, 
	IDataItemContainer, INamingContainer
<asp:ListViewDataItem />

The ListViewDataItem class represents an individual data item in a ListView control. A data item usually corresponds to a record in a data source object. The class derives from the ListViewItem class.

Each item in the ListView control has a designated item type that is represented by the ItemType property. When the item type is ListViewItemType.DataItem, you can use the ListViewDataItem class to get more information about the item.

The ListView control stores data items in the Items collection. To determine the index of a ListViewDataItem object in the Items collection, use the DisplayIndex property. To determine the index of the data item in the underlying data source, use the DataItemIndex property. The difference between these index properties is that DisplayIndex represents the position of the data item in the current page, whereas DataItemIndex is based on the page offset. If the paging functionality is not enabled in the ListView control by using a DataPager control, these two properties will contain the same value.

You can access the properties of the underlying data object by using the DataItem property.

The DataItem property is available only during and after the ItemDataBound event of a ListView control.

For a list of initial property values for an instance of the ListViewDataItem class, see the ListViewDataItem constructor.

The following example shows how to use a ListViewDataItem object to retrieve a field value from an item in the ListView control.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<script runat="server">

  protected void ContactsListView_ItemDataBound(object sender, ListViewItemEventArgs e)

    //Verify there is an item being edited. 
    if (ContactsListView.EditIndex >= 0)

      //Get the item object.
      ListViewDataItem dataItem = (ListViewDataItem)e.Item;

      // Check for an item in edit mode. 
      if (dataItem.DisplayIndex == ContactsListView.EditIndex)

        // Preselect the DropDownList control with the Title value 
        // for the current item. 

        // Retrieve the underlying data item. In this example 
        // the underlying data item is a DataRowView object.        
        DataRowView rowView = (DataRowView)dataItem.DataItem;

        // Retrieve the Title value for the current item. 
        String title = rowView["Title"].ToString();

        // Retrieve the DropDownList control from the current row. 
        DropDownList list = (DropDownList)dataItem.FindControl("TitlesList");

        // Find the ListItem object in the DropDownList control with the  
        // title value and select the item.
        ListItem item = list.Items.FindByText(title);
        list.SelectedIndex = list.Items.IndexOf(item);


  void ContactsListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
      // Retrieve the row being edited.
      ListViewItem item = ContactsListView.Items[ContactsListView.EditIndex];

      // Retrieve the DropDownList control from the row.
      DropDownList list = (DropDownList)item.FindControl("TitlesList");

      // Add the selected value of the DropDownList control to  
      // the NewValues collection. The NewValues collection is 
      // passed to the data source control, which then updates the  
      // data source.
      e.NewValues["Title"] = list.SelectedValue;

<html xmlns="" >
  <head id="Head1" runat="server">
    <title>ListView DataItem Example</title>
    <link type="text/css" rel="stylesheet" href="StyleSheet.css" />
    <form id="form1" runat="server">

      <h3>ListViewDataItem Example</h3>

      <asp:ListView ID="ContactsListView" 
      	runat="server" >
          <table cellpadding="2" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          <asp:DataPager runat="server" ID="ContactsDataPager" PageSize="12">
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
          <tr class="DataItem" runat="server">
              <asp:LinkButton ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
              <asp:Label ID="TitleLabel" runat="server" Text='<%#Eval("Title", "{0:d}") %>' />
          <tr class="EditItem">
              <asp:LinkButton ID="UpdateButton" runat="server" 
                CommandName="Update" Text="Update" /><br />
              <asp:LinkButton ID="CancelButton" runat="server" 
                CommandName="Cancel" Text="Cancel" />
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name:"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name:" />
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="TitleLabel" 
                AssociatedControlID="TitlesList" Text="Title:"/>
              <asp:DropDownList ID="TitlesList" 

      <asp:SqlDataSource ID="TitlesSqlDataSource"  
        SelectCommand="SELECT Distinct [Title] FROM Person.Contact ORDER BY [Title]"
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"

      <asp:SqlDataSource ID="ContactsDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
            SelectCommand="SELECT [ContactID], [FirstName], [LastName], [Title] FROM Person.Contact"
            UpdateCommand="UPDATE Person.Contact
                             SET FirstName = @FirstName, LastName = @LastName, Title = @Title
                             WHERE ContactID = @ContactID">
          <asp:Parameter ConvertEmptyStringToNull="true" Name="Title" />



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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5