Compartilhar via


ListView.EditIndex Propriedade

Definição

Obtém ou define o índice do item que está sendo 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 da propriedade

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

Exceções

A propriedade EditIndex é definida com um valor menor que -1.

Exemplos

O exemplo a seguir mostra como usar a EditIndex propriedade para determinar se um item está no modo de edição no ListView controle . 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

Comentários

Você pode usar a EditIndex propriedade para especificar ou determinar programaticamente qual item em um ListView controle editar. Quando essa propriedade é definida como o índice de um item no controle , esse item é exibido no modo de edição. No modo de edição, o item é renderizado usando o EditItemTemplate modelo em vez do ItemTemplate modelo. Você pode preencher o EditItemTemplate com controles associados a dados para permitir que os usuários modifiquem valores para o item. Para alternar do modo de edição para o modo de exibição, defina essa propriedade como -1.

A EditIndex propriedade normalmente é usada quando você precisa determinar programaticamente qual item está sendo editado ou quando você está adicionando funcionalidade de edição personalizada ao ListView controle. O ListView controle tem um recurso de edição interno que coloca automaticamente um item no modo de edição se você adicionar um botão ao modelo de item cuja CommandName propriedade está definida Editcomo .

Aplica-se a

Confira também