ListViewDataItem.DataItem 属性

定义

获取或设置 ListViewItem 对象绑定到的基础数据对象。

C#
public virtual object DataItem { get; set; }
C#
public override object DataItem { get; set; }

属性值

ListViewItem 对象绑定到的基础数据对象。

示例

以下示例演示如何使用 DataItem 属性检索字段值。 然后,该值用于预先选择控件中的 DropDownList 项,当项处于编辑模式时显示。 此代码示例是为 ListViewDataItem 类提供的一个更大示例的一部分。

C#
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);
                      
    }
  }
}

注解

属性DataItem仅在 控件事件ListView期间和之后ItemDataBound可用。

适用于

产品 版本
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅