ListView.EditIndex Proprietà

Definizione

Ottiene o imposta l'indice dell'elemento da modificare.

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

Valore della proprietà

Int32

Indice in base zero dell'elemento da modificare. Il valore predefinito è -1 e indica che non viene modificato alcun elemento.

Eccezioni

La proprietà EditIndex è impostata su un valore minore di -1.

Esempio

Nell'esempio seguente viene illustrato come usare la EditIndex proprietà per determinare se un elemento è in modalità di modifica nel ListView controllo. Questo esempio di codice fa parte di un esempio più grande fornito per la 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

Commenti

È possibile usare la proprietà per specificare o determinare l'elemento EditIndex in un ListView controllo da modificare a livello di codice. Quando questa proprietà è impostata sull'indice di un elemento nel controllo, tale elemento viene visualizzato in modalità di modifica. In modalità di modifica, l'elemento viene eseguito tramite il modello anziché il EditItemTemplate ItemTemplate modello. È possibile popolare l'oggetto EditItemTemplate con controlli associati a dati per consentire agli utenti di modificare i valori per l'elemento. Per passare dalla modalità di modifica alla modalità di visualizzazione, impostare questa proprietà su -1.

La EditIndex proprietà viene in genere usata quando è necessario determinare a livello di codice quale elemento viene modificato o quando si aggiunge la funzionalità di modifica personalizzata al ListView controllo. Il ListView controllo ha una funzionalità di modifica predefinita che inserisce automaticamente un elemento in modalità di modifica se si aggiunge un pulsante al modello di elemento la cui CommandName proprietà è impostata su Edit.

Si applica a

Vedi anche