ListViewSelectEventArgs.NewSelectedIndex Property

Definition

Gets or sets the index of the new item to select in the ListView control.

public:
 property int NewSelectedIndex { int get(); void set(int value); };
public int NewSelectedIndex { get; set; }
member this.NewSelectedIndex : int with get, set
Public Property NewSelectedIndex As Integer

Property Value

The index of the new item to select in the ListView control.

Examples

The following example shows how to use the NewSelectedIndex property of the ListViewSelectEventArgs object to access the item that was selected by the user.

void ProductsListView_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
  ListViewItem item = (ListViewItem)ProductsListView.Items[e.NewSelectedIndex];
  Label l = (Label)item.FindControl("DiscontinuedDateLabel");

  if (String.IsNullOrEmpty(l.Text))
  {
    return;
  }

  DateTime discontinued = DateTime.Parse(l.Text);
  if (discontinued < DateTime.Now)
  {
    Message.Text = "You cannot select a discontinued item.";
    e.Cancel = true;
  }
}
Sub ProductsListView_SelectedIndexChanging(ByVal sender As Object, ByVal e As ListViewSelectEventArgs)

  Dim item As ListViewItem = CType(ProductsListView.Items(e.NewSelectedIndex), ListViewItem)  
  Dim l As Label = CType(item.FindControl("DiscontinuedDateLabel"), Label)

  If String.IsNullOrEmpty(l.Text) Then
    Return
  End If

  Dim discontinued As DateTime = DateTime.Parse(l.Text)
  If discontinued < DateTime.Now Then      
    Message.Text = "You cannot select a discontinued item."
    e.Cancel = True
  End If
End Sub

Remarks

The ListView.SelectedIndexChanging event occurs before the ListView control performs the select operation. Therefore, you cannot use the ListView.SelectedIndex property of the control to determine the index of the new item selected by the user. The ListView.SelectedIndex property contains the index of the previously selected item. To determine the index of the new item that is selected by the user, use the NewSelectedIndex property. You can also use this property to programmatically override the selected item index by setting it to another value.

Applies to

See also