ASP.Net C# DropDownList SelectedIndex Property

Updated on 04 Jul 2012,
Published on 28 Oct 2009

The SelectedIndex property of ASP.Net DropDownList control enables you to get or set the zero-based index of the selected list item dynamically using C# code. When user clicks on any list item of dropdownlist control it fires the OnSelectedIndexChanged event that can be handled at server side to get the index of the list item clicked by the user. There you can use the SelectedIndex property that returns the integer type value associated to the list item selected by the user. It also allows you to set the list item at the specified index in selected state programmatically. Using SelectedIndex property in the C# code you can also access the properties of Items collection property of dropdownlist control.

Following are the properties of Items collection that can be accessed using SelectedIndex property of dropdownlist control:

1. Attributes: allows you to add the attributes to the associated list item dynamically.

2. Enabled: it allows you to enable or disable the selection of list item based on its Boolean type true/false value.

3. Selected: it sets the list item in selected state based on its true or false Boolean value.

4. Text: it allows you to get or set the text content of the selected list item of dropdownlist control.

5. Value: it allows you to get or set the value of selected list item dynamically.

As SelectedIndex property is an integer type property, you can specify the integer type value to set the list item of dropdownlist controls in selected state. If you will specify the integer value more the maximum index of dropdownlist control items then it will throw an error: "System.ArgumentOutOfRangeException: 'DropDownList1' has a SelectedIndex which is invalid because it does not exist in the list of items. Parameter name: value". Before specifying the value for SelectedIndex property you must ensure that it must greater than or equal to 0 and less than the maximum index number of dropdownlist control. By default the SelectedIndex property has value 0 that selects the item at index 0 i.e. the first list item. Due to common ListControl base class for list type controls it also supports "-1" index value but you are recommended not to use it for DropDownList control.

You can use the following code to get the SelectedIndex of list item of dropdownlist control selected by the user:

Response.Write(DropDownList1.SelectedIndex);

Similarly you can assign the integer value to the SelectedIndex property to select the list item programmatically:

DropDownList1.SelectedIndex = 3;

For accessing the properties of Items collection property associated to the selected list item using SelectedIndex property you can use the following C# code:

Response.Write(DropDownList1.Items[DropDownList1.SelectedIndex].Value);

Similarly you can get or set the values for different properties of Items property of dropdownlist control as we discussed earlier in this tutorial such as Attributes, Enabled, Selected, Text and Value.

Output:

Get the output for above discussed code from the following link:

DropDownList Control SelectedIndex Property

Continue to the next tutorial: ASP.Net DropDownList Validation using JavaScript to learn how to apply the client side validation to DropDownList server control.

0 Responses to "ASP.Net C# DropDownList SelectedIndex Property"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email