asp.net.ph

DataList.SelectedIndex Property

System.Web.UI.WebControls Namespace   DataList Class


Sets or retrieves the index of the selected item in a DataList control.

Syntax


Script DataList.SelectedIndex [ = intIndex ]

This property can only be used programmatically; it cannot be set when declaring the control.

Property Value


intIndex Integer specifying the ordinal index of the selected item.

The property is read/write with no default value.

Exceptions


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.

Remarks

Items in a DataList are indexed in the order in which they are defined in the data source, starting with an index of zero.

The SelectedIndex property is mainly used to programmatically specify or determine the index of the selected item in a DataList. This index can then be used to access the methods and properties of the selected item.

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. You can programmatically deselect an item 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.

Example

The following example demonstrates using the SelectedIndex property to determine the selected item in a DataList control.

void myListSelectHandler ( Object src, EventArgs e ) {
   string title = myList.DataKeys [ myList.SelectedIndex ].ToString ( );
   string query = "Select * FROM pubs_Titles where Title_id = '" + title + "'";
   myDetailsList.DataSource = fetchData ( query, "pubs" );
   myDetailsList.DataBind ( );
}
  C# VB

 Show me 

See Also

DataList Members   SelectedIndexChanged   SelectedItem   SelectedItemTemplate   SelectedItemStyle   Allowing Users to Select Items in a DataList Control Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph