System.Web.UI.WebControls Namespace ListControl Class
Sets or retrieves an integer specifying the index of the selected item in a ListControl.
Inline |
<asp:ListControl selectedindex = intIndex ... > |
Script |
ListControl.SelectedIndex [ = intIndex ] |
intIndex |
Integer specifying the ordinal index of the selected item in a ListControl. |
The property is read/write with a default value of -1, which indicates that nothing is selected.
Exception Type |
Condition |
ArgumentOutOfRangeException |
The given index is set to less than -1 or greater than or equal to the items count of the list. |
The list items in a ListControl are indexed in the order in which they are defined, starting with an index of zero.
Use the SelectedIndex property to specify or determine the index of the selected item in a single-selection ListControl. This index can then be used to access the properties of the selected list item from the Items collection.
If SelectionMode is set to multiple, this property returns only the index of the first selected item ( the lowest index in the selection ).
By default, the first item in the list is initially selected. When multiple selection is enabled, though, it is possible to have no item selected. This commonly occurs when the page first loads and no item is selected by default.
SelectedIndex returns a value of -1 if no item is selected. Always provide code to test this value before attempting to reference any selected item in your code.
If a multiple selection list box is displayed, you can programmatically deselect all items by setting the SelectedIndex property to -1.
NOTE: Like most indexes in ASP.NET, SelectedIndex is zero-based, meaning the first item in the collection returns an index of 0.
Below are links to code examples that illustrate using a list control's SelectedIndex property in varied scenarios.
ListControl Members SelectedIndexChanged SelectedItem SelectedValue