Bagikan melalui


ListView.EditIndex Properti

Definisi

Mendapatkan atau mengatur indeks item yang sedang diedit.

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

Nilai Properti

Indeks berbasis nol dari item yang sedang diedit. Defaultnya adalah -1, yang menunjukkan bahwa tidak ada item yang sedang diedit.

Pengecualian

Properti EditIndex diatur ke nilai kurang dari -1.

Contoh

Contoh berikut menunjukkan cara menggunakan EditIndex properti untuk menentukan apakah item berada dalam mode edit dalam ListView kontrol. Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk ListViewDataItem kelas .

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

Keterangan

Anda dapat menggunakan EditIndex properti untuk menentukan atau menentukan item mana dalam kontrol yang akan diedit ListView secara terprogram. Ketika properti ini diatur ke indeks item dalam kontrol, item tersebut ditampilkan dalam mode edit. Dalam mode edit, item dirender dengan menggunakan EditItemTemplate templat alih-alih ItemTemplate templat. Anda dapat mengisi EditItemTemplate dengan kontrol terikat data untuk memungkinkan pengguna mengubah nilai item. Untuk beralih dari mode edit ke mode tampilan, atur properti ini ke -1.

Properti EditIndex biasanya digunakan ketika Anda harus secara terprogram menentukan item mana yang sedang diedit, atau saat Anda menambahkan fungsionalitas pengeditan kustom ke ListView kontrol. Kontrol ListView memiliki fitur pengeditan bawaan yang secara otomatis menempatkan item dalam mode edit jika Anda menambahkan tombol ke templat item yang propertinya CommandName diatur ke Edit.

Berlaku untuk

Lihat juga