ListView.EditIndex Propiedad

Definición

Obtiene o establece el índice del elemento que va a editarse.

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 propiedad

Int32

Índice de base cero del elemento que va a editarse. El valor predeterminado es -1, que indica que no se edita ningún elemento.

Excepciones

La propiedad EditIndex está establecida en un valor menor que -1.

Ejemplos

En el ejemplo siguiente se muestra cómo usar la EditIndex propiedad para determinar si un elemento está en modo de edición en el ListView control . Este ejemplo de código forma parte de un ejemplo más grande proporcionado para la ListViewDataItem clase .

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

Comentarios

Puede usar la EditIndex propiedad para especificar o determinar mediante programación qué elemento de un ListView control se va a editar. Cuando esta propiedad se establece en el índice de un elemento del control , ese elemento se muestra en modo de edición. En el modo de edición, el elemento se representa mediante la EditItemTemplate plantilla en lugar de la ItemTemplate plantilla. Puede rellenar con EditItemTemplate controles enlazados a datos para permitir que los usuarios modifiquen los valores del elemento. Para cambiar del modo de edición al modo de presentación, establezca esta propiedad en -1.

La EditIndex propiedad se usa normalmente cuando se tiene que determinar mediante programación qué elemento se está editando o cuando se agrega la funcionalidad de edición personalizada al ListView control. El ListView control tiene una característica de edición integrada que coloca automáticamente un elemento en modo de edición si agrega un botón a la plantilla de elemento cuya CommandName propiedad está establecida Editen .

Se aplica a

Consulte también