Expand Minimize

ListView.SelectedIndex Property

Updated: May 2011

Gets or sets the index of the selected item in a ListView control.

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

public virtual int SelectedIndex { get; set; }
<asp:ListView SelectedIndex="Int32" />

Property Value

Type: System.Int32
The zero-based index of the selected item in a ListView control. The default is -1, which indicates that no item is currently selected.

ExceptionCondition
ArgumentOutOfRangeException

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

Use the SelectedIndex property to determine the index of the currently selected item in a ListView control. You can also use this property to programmatically select an item in the control. (However, if you manually data-bind the ListView control, you might have to handle reading or setting this property manually as well.)

The following example shows how to use the SelectedIndex property to determine whether an item is selected in a ListView control. This code example is part of a larger example provided for the DeleteItem method.


protected void DeleteButton_Click(object sender, EventArgs e)
{
  //Check if an item is selected to delete it.
  if (ContactsListView.SelectedIndex >= 0)
    ContactsListView.DeleteItem(ContactsListView.SelectedIndex);
  else
    Message.Text = "No contact was selected.";
}


.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

May 2011

Added an explanation that the property does not always work as described here when data-binding is performed manually.

Customer feedback.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft