ListView.EditIndex Vlastnost

Definice

Získá nebo nastaví index položky, která se upravuje.

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

Hodnota vlastnosti

Int32

Index položky, který se upravuje na základě nuly. Výchozí hodnota je -1, což značí, že se neupravuje žádná položka.

Výjimky

Vlastnost EditIndex je nastavena na hodnotu menší než -1.

Příklady

Následující příklad ukazuje, jak pomocí EditIndex vlastnosti určit, zda je položka v režimu úprav v ovládacím ListView prvku. Tento příklad kódu je součástí většího příkladu poskytnutého ListViewDataItem pro třídu.

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

Poznámky

Vlastnost můžete použít EditIndex k programovému určení nebo určení položky v ovládacím ListView prvku, který chcete upravit. Pokud je tato vlastnost nastavena na index položky v ovládacím prvku, zobrazí se tato položka v režimu úprav. V režimu úprav se položka vykreslí pomocí EditItemTemplate šablony místo ItemTemplate šablony. Můžete naplnit EditItemTemplate ovládacími prvky vázanými na data, aby uživatelé mohli upravovat hodnoty pro danou položku. Pokud chcete přepnout z režimu úprav na režim zobrazení, nastavte tuto vlastnost na -1.

Vlastnost EditIndex se obvykle používá, když potřebujete programově určit, kterou položku upravujete, nebo když do ListView ovládacího prvku přidáváte vlastní funkce úprav. Ovládací ListView prvek má integrovanou funkci pro úpravy, která automaticky umístí položku do režimu úprav, pokud přidáte tlačítko do šablony položky, jejíž CommandName vlastnost je nastavena na Edit.

Platí pro

Viz také