ListViewDataItem.DataItem 属性
本文内容
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 ListViewItem 对象绑定到的基础数据对象。
public:
virtual property System::Object ^ DataItem { System::Object ^ get(); void set(System::Object ^ value); };
C#
public virtual object DataItem { get; set; }
C#
public override object DataItem { get; set; }
member this.DataItem : obj with get, set
Public Overridable Property DataItem As Object
Public Overrides Property DataItem As Object
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);
}
}
}
Protected Sub ContactsListView_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
'Verify there is an item being edited.
If ContactsListView.EditIndex >= 0 Then
'Get the item object.
Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
' Check for an item in edit mode.
If dataItem.DisplayIndex = ContactsListView.EditIndex Then
' 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.
Dim rowView As DataRowView = CType(dataItem.DataItem, DataRowView)
' Retrieve the Title value for the current item.
Dim title As String = rowView("Title").ToString()
' Retrieve the DropDownList control from the current row.
Dim list As DropDownList = CType(dataItem.FindControl("TitlesList"), DropDownList)
' Find the ListItem object in the DropDownList control with the
' title value and select the item.
Dim item As ListItem = list.Items.FindByText(title)
list.SelectedIndex = list.Items.IndexOf(item)
End If
End If
End Sub
属性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 |