ListView.EditIndex 屬性

定義

取得或設定正在編輯之項目的索引。

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

屬性值

Int32

正在編輯之項目以零起始的索引。 預設為 -1,表示目前未編輯任何項目。

例外狀況

EditIndex 屬性會設定為小於 -1 的值。

範例

下列範例示範如何使用 EditIndex 屬性來判斷專案是否處於 控制項的 ListView 編輯模式。 此程式碼範例是針對 類別提供的較大範例的 ListViewDataItem 一部分。

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

備註

您可以使用 EditIndex 屬性,以程式設計方式指定或決定控制項中 ListView 要編輯的專案。 當此屬性設定為 控制項中專案的索引時,該專案會顯示在編輯模式中。 在編輯模式中,專案會使用 EditItemTemplate 範本來轉譯,而不是 ItemTemplate 範本。 您可以使用資料繫結控制項填入 , EditItemTemplate 讓使用者修改專案的值。 若要從編輯模式切換至顯示模式,請將此屬性設定為 -1。

當您 EditIndex 必須以程式設計方式判斷要編輯的專案,或當您將自訂編輯功能新增至 ListView 控制項時,通常會使用 屬性。 如果您將按鈕新增至屬性設定 Edit 為 的專案範本 CommandName ,控制項 ListView 具有內建的編輯功能,會自動將專案置於編輯模式。

適用於

另請參閱