ListView.EditIndex 属性

定义

获取或设置所编辑的项的索引。

public:
 virtual property int EditIndex { int get(); void set(int value); };
public virtual int EditIndex { get; set; }
member this.EditIndex : int with get, set
Public Overridable Property EditIndex As Integer

属性值

Int32

所编辑的项的从零开始的索引。 默认值为 -1,表示没有正在编辑的项。

例外

EditIndex 属性被设置为小于 -1 的值。

示例

以下示例演示如何使用 EditIndex 属性来确定某个项是否处于控件中的 ListView 编辑模式。 此代码示例是为类提供的大型示例的 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

注解

可以使用 EditIndex 该属性以编程方式指定或确定要编辑的 ListView 控件中的项。 当此属性设置为控件中项的索引时,该项会显示在编辑模式下。 在编辑模式下,使用模板而不是ItemTemplate模板呈现EditItemTemplate项。 可以使用数据绑定控件填充 EditItemTemplate ,使用户能够修改项的值。 若要从编辑模式切换到显示模式,请将此属性设置为 -1。

EditIndex当必须以编程方式确定要编辑的项或向控件添加自定义编辑功能ListView时,通常会使用该属性。 如果向属性设置为Edit的项模板CommandName添加按钮,控件ListView具有内置的编辑功能,该功能会自动将项目置于编辑模式。

适用于

另请参阅