ListView.EditIndex Propriedade

Definição

Obtém ou define o índice do item que está a ser editado.

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

Valor de Propriedade

O índice em base zero do item a ser editado. O padrão é -1, o que indica que nenhum item está a ser editado.

Exceções

A EditIndex propriedade é definida para um valor inferior a -1.

Exemplos

O exemplo seguinte mostra como usar a EditIndex propriedade para determinar se um item está em modo de edição no ListView controlo. Este exemplo de código faz parte de um exemplo maior fornecido para a ListViewDataItem classe.

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

Observações

Podes usar a EditIndex propriedade para especificar ou determinar programaticamente que item num ListView controlo editar. Quando esta propriedade está definida para o índice de um item no controlo, esse item é exibido em modo de edição. No modo de edição, o item é renderizado usando o EditItemTemplate template em vez do template.ItemTemplate Pode preencher o EditItemTemplate com controlos ligados a dados para permitir que os utilizadores modifiquem valores para o item. Para mudar do modo de edição para o modo de exibição, defina esta propriedade para -1.

A EditIndex propriedade é normalmente usada quando tens de determinar programaticamente qual elemento está a ser editado, ou quando estás a adicionar funcionalidades de edição personalizadas ao ListView controlo. O ListView controlo tem uma funcionalidade de edição incorporada que coloca automaticamente um item em modo de edição se adicionar um botão ao modelo cuja CommandName propriedade está definida como Edit.

Aplica-se a

Ver também