How to: Determine the Selection in List Web Server Controls
One of the most common tasks in working with a list Web server control is to determine what item or items users have selected. The procedure varies depending on whether the list control allows single or multiple selections.
To determine the selection in a single-selection list control
Use one of the following methods:
To get the index value of the selected item, read the value of the SelectedIndex property. The index is zero-based. If nothing has been selected, the value of the property is -1.
To get the contents of the selected item, get the control's SelectedItem property. This property returns an object of type ListItem. You can get the contents of the selected item by getting the Text or Value property of the object.
Controls in a Web Forms page can include potentially malicious client script. By default, the Web Forms page validates that user input does not include script or HTML elements. For more information, see How to: Protect Against Script Exploits in a Web Application by Applying HTML Encoding to Strings
The following code example shows how you can test which item is selected in a RadioButtonList control. The code first determines whether there is a selection at all by reading the value of the SelectedIndex property, which is set to -1 until the user selects an item. It then gets the SelectedItem object and displays that object's Text property.
If the list control supports multiple selections, you must loop through the control and check for selected items one by one.