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.

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

属性值

IList<ListViewDataItem>

一个对象,包含 ListView 控件中的当前数据页的所有数据项。An object that contains all the data items of the current page of data in a ListView control.

属性

示例

下面的示例演示如何使用 Items 集合来访问控件中正在编辑的项 ListViewThe following example shows how to use the Items collection to access the item that is being edited in a ListView control. 此代码示例是为事件提供的更大示例的一部分 ItemEditingThis 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))
    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;
  }       
}
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 _
    Return
    
  '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

注解

Items属性用于在控件中存储数据项 ListViewThe Items property is used to store the data items in a ListView control. ListView控件 Items 通过 ListViewDataItem 为数据源中的当前数据页中的每个记录创建一个对象来自动填充该集合。The 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.

适用于

另请参阅