ListView.Items 屬性


取得 ListViewDataItem 物件的集合,這些物件表示 ListView 控制項中目前資料頁的資料項目。Gets a collection of ListViewDataItem objects that represent the data items of the current page of data in a ListView control.

 virtual property System::Collections::Generic::IList<System::Web::UI::WebControls::ListViewDataItem ^> ^ Items { System::Collections::Generic::IList<System::Web::UI::WebControls::ListViewDataItem ^> ^ get(); };
public virtual System.Collections.Generic.IList<System.Web.UI.WebControls.ListViewDataItem> Items { get; }
member this.Items : System.Collections.Generic.IList<System.Web.UI.WebControls.ListViewDataItem>
Public Overridable ReadOnly Property Items As IList(Of ListViewDataItem)


物件,內含 ListView 控制項中目前資料頁面的所有資料項目。An object that contains all the data items of the current page of data in a ListView control.



下列範例示範如何使用Items集合來存取ListView在控制項中編輯的專案。The following example shows how to use the Items collection to access the item that is being edited in a ListView control. 這個程式碼範例是針對ItemEditing事件提供的較大範例的一部分。This code example is part of a larger example provided for the ItemEditing event.

void ProductsListView_ItemEditing(Object sender, ListViewEditEventArgs e)
  ListViewItem item = ProductsListView.Items[e.NewEditIndex];
  Label dateLabel = (Label)item.FindControl("DiscontinuedDateLabel");
  if (String.IsNullOrEmpty(dateLabel.Text))
  //Verify if the item is discontinued.
  DateTime discontinuedDate = DateTime.Parse(dateLabel.Text);
  if (discontinuedDate < DateTime.Now)
    Message.Text = "You cannot edit a discontinued item.";
    e.Cancel = true;
    ProductsListView.SelectedIndex = -1;
Sub ProductsListView_ItemEditing(ByVal sender As Object, ByVal e As ListViewEditEventArgs)
  Dim item As ListViewItem = ProductsListView.Items(e.NewEditIndex)
  Dim dateLabel As Label = CType(item.FindControl("DiscontinuedDateLabel"), Label)
  If String.IsNullOrEmpty(dateLabel.Text) Then _
  'Verify if the item is discontinued.
  Dim discontinuedDate As DateTime = DateTime.Parse(dateLabel.Text)
  If discontinuedDate < DateTime.Now Then
    Message.Text = "You cannot edit a discontinued item."
    e.Cancel = True
    ProductsListView.SelectedIndex = -1
  End If
End Sub


屬性是用來儲存ListView控制項中的資料項目。 ItemsThe Items property is used to store the data items in a ListView control. 控制項會為數據源中Items目前資料頁內ListViewDataItem的每一筆記錄建立一個物件, 以自動填入集合。 ListViewThe ListView control automatically populates the Items collection by creating one ListViewDataItem object for each record in the current page of data in the data source. 然後, 它會將每個物件新增至集合。It then adds each object to the collection. 這個屬性通常是用來存取控制項中的特定專案, 或是逐一查看完整的專案集合。This property is usually used to access a specific item in the control or to iterate though the complete collection of items.