ListView.Items 屬性

定義

取得 ListViewDataItem 物件的集合,這些物件表示 ListView 控制項中目前資料頁的資料項目。

[System.ComponentModel.Browsable(false)]
public virtual System.Collections.Generic.IList<System.Web.UI.WebControls.ListViewDataItem> Items { get; }

屬性值

物件,內含 ListView 控制項中目前資料頁面的所有資料項目。

屬性

範例

下列範例示範如何使用 Items 集合來存取控件中 ListView 正在編輯的專案。 此程式代碼範例是針對 事件提供的較大範例的 ItemEditing 一部分。

void ProductsListView_ItemEditing(Object sender, ListViewEditEventArgs e)
{
  ListViewItem item = ProductsListView.Items[e.NewEditIndex];
  Label dateLabel = (Label)item.FindControl("DiscontinuedDateLabel");
  
  if (String.IsNullOrEmpty(dateLabel.Text))
    return;
  
  //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;
  }       
}

備註

屬性 Items 是用來將數據項儲存在控件中 ListView 。 控件 ListView 會自動填 Items 入集合,方法是為數據源中目前數據頁面中的每一筆記錄建立一個 ListViewDataItem 物件。 然後,它會將每個物件新增至集合。 此屬性通常用來存取 控件中的特定專案,或逐一查看專案的完整集合。

適用於

產品 版本
.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

另請參閱