ListView.EditIndex Właściwość

Definicja

Pobiera lub ustawia indeks edytowanego elementu.

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

Wartość właściwości

Int32

Indeks oparty na zerze edytowanego elementu. Wartość domyślna to -1, co oznacza, że żaden element nie jest edytowany.

Wyjątki

Właściwość jest ustawiona EditIndex na wartość mniejszą niż -1.

Przykłady

W poniższym przykładzie pokazano, jak za pomocą EditIndex właściwości określić, czy element jest w trybie edycji w kontrolce ListView . Ten przykład kodu jest częścią większego przykładu udostępnionego ListViewDataItem dla klasy .

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

Uwagi

Za pomocą EditIndex właściwości można programowo określić lub określić, który element w kontrolce ListView ma być edytowany. Gdy ta właściwość jest ustawiona na indeks elementu w kontrolce, ten element jest wyświetlany w trybie edycji. W trybie edycji element jest renderowany przy użyciu EditItemTemplate szablonu zamiast szablonu ItemTemplate . Kontrolki EditItemTemplate powiązane z danymi można wypełnić, aby umożliwić użytkownikom modyfikowanie wartości elementu. Aby przełączyć się z trybu edycji na tryb wyświetlania, ustaw tę właściwość na -1.

Właściwość EditIndex jest zwykle używana, gdy trzeba programowo określić, który element jest edytowany, lub podczas dodawania niestandardowej funkcji edycji do kontrolki ListView . Kontrolka ListView ma wbudowaną funkcję edycji, która automatycznie umieszcza element w trybie edycji, jeśli dodasz przycisk do szablonu elementu, którego CommandName właściwość jest ustawiona na Editwartość .

Dotyczy

Zobacz też