ListViewDataItem.DataItem 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 ListViewItem 物件所繫結的基礎資料物件。
public:
virtual property System::Object ^ DataItem { System::Object ^ get(); void set(System::Object ^ value); };
public virtual object DataItem { get; set; }
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 一部分。
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 使用。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應