System.Web.UI.WebControls.ListControl.SelectedValue Property

Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value.

Syntax

[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
[System.ComponentModel.DefaultValue("")]
[System.ComponentModel.Browsable(false)]
[System.Web.UI.Themeable(false)]
[System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)]
public virtual string SelectedValue { set; get; }

Value

a string

Remarks

This property returns the ListItem.Value property of the selected System.Web.UI.WebControls.ListItem. The ListControl.SelectedValue property is commonly used to determine the value of the selected item in the list control. If multiple items are selected, the value of the selected item with the lowest index is returned. If no item is selected, an empty string ("") is returned.

The ListControl.SelectedValue property can also be used to select an item in the list control by setting it with the value of the item.

This property cannot be set by themes or style sheet themes. For more information, see System.Web.UI.ThemeableAttribute and ASP.NET Themes Overview.

When the selected value is not in the list of available values and a postback is performed, an ArgumentOutOfRangeException exception is thrown. The following example shows how to catch an invalid value before postback occurs:

Example

Me.DropDownList1.Items.Add(New ListItem( Text="Hello", Value="1" )) 
If DropDownList1.Items.FindByValue("2") IsNot Nothing Then 
    Response.Write("Found") 
End If

Example

this.DropDownList1.Items.Add(new ListItem{ Text="Hello", Value="1" });
if(DropDownList1.Items.FindByValue("2") != null) {
    Response.Write("Found");
}

Requirements

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0