ListViewDataItem.DataItem Property

 

Gets or sets the underlying data object that the ListViewItem object is bound to.

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

public override object DataItem { get; set; }

Property Value

Type: System.Object

The underlying data object that the ListViewItem object is bound to.

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

The following example shows how to use the DataItem property to retrieve a field value. The value is then used to pre-select an item in a DropDownList control that is displayed when an item is in edit mode. This code example is part of a larger example that is provided for the ListViewDataItem class.

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);

    }
  }
}

.NET Framework
Available since 3.5
Return to top
Show: