ListViewDataItem.DataItem Eigenschaft

Definition

Ruft das zugrunde liegende Datenobjekt ab, an das das ListViewItem-Objekt gebunden ist, oder legt dieses fest.

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

Eigenschaftswert

Das zugrunde liegende Datenobjekt, an das das ListViewItem-Objekt gebunden ist.

Beispiele

Das folgende Beispiel zeigt, wie Sie die DataItem -Eigenschaft verwenden, um einen Feldwert abzurufen. Der Wert wird dann verwendet, um ein Element in einem DropDownList Steuerelement vorab auszuwählen, das angezeigt wird, wenn sich ein Element im Bearbeitungsmodus befindet. Dieses Codebeispiel ist Teil eines größeren Beispiels, das für die ListViewDataItem-Klasse bereitgestellt wird.

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

Hinweise

Die DataItem Eigenschaft ist nur während und nach dem ItemDataBound Ereignis eines ListView Steuerelements verfügbar.

Gilt für:

Weitere Informationen