Duration (Pacific Standard Time):
To (Pacific Standard Time):
User Action:

ListView.EditIndex Property


Gets or sets the index of the item being edited.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

Public Overridable Property EditIndex As Integer

Property Value

Type: System.Int32

The zero-based index of the item being edited. The default is -1, which indicates that no item is being edited.

Exception Condition

The EditIndex property is set to a value less than -1.

You can use the EditIndex property to programmatically specify or determine which item in a ListView control to edit. When this property is set to the index of an item in the control, that item is displayed in edit mode. In edit mode, the item is rendered by using the EditItemTemplate template instead of the ItemTemplate template. You can populate the EditItemTemplate with data-bound controls to enable users to modify values for the item. To switch from edit mode to display mode, set this property to -1.

The EditIndex property is typically used when you have to programmatically determine which item is being edited, or when you are adding custom editing functionality to the ListView control. The ListView control has a built-in editing feature that automatically puts an item in edit mode if you add a button to the item template whose CommandName property is set to Edit.

The following example shows how to use the EditIndex property to determine whether an item is in edit mode in the ListView control. This code example is part of a larger example provided for the ListViewDataItem class.

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
.NET Framework
Available since 3.5
Return to top