Export (0) Print
Expand All

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" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView DataItem Example</title>
    <link type="text/css" rel="stylesheet" href="StyleSheet.css" />
  </head>
  <body>
    <form id="form1" runat="server">

      <h3>ListViewDataItem Example</h3>

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

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

      <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">
        <UpdateParameters>
          <asp:Parameter ConvertEmptyStringToNull="true" Name="Title" />
        </UpdateParameters>                             
      </asp:SqlDataSource>

    </form>
  </body>
</html>

System.Object
  System.Web.UI.Control
    System.Web.UI.WebControls.ListViewItem
      System.Web.UI.WebControls.ListViewDataItem

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

Community Additions

ADD
Show:
© 2014 Microsoft