ListControl.SelectedValue Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value.
public:
virtual property System::String ^ SelectedValue { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public virtual string SelectedValue { get; set; }
[System.ComponentModel.Browsable(false)]
[System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)]
[System.Web.UI.Themeable(false)]
public virtual string SelectedValue { get; set; }
[]
[]
member this.SelectedValue : string with get, set
[]
[]
[]
member this.SelectedValue : string with get, set
Public Overridable Property SelectedValue As String
Property Value
The value of the selected item in the list control. The default is an empty string ("").
- Attributes
Exceptions
The selected value is not in the list of available values and view state or other state has been loaded (a postback has been performed).
Examples
The following example demonstrates how to use the SelectedValue property to select an item in a ListBox control. Notice that this property can also be used to retrieve the value of the selected item.
Important
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.
<%@ Page Language="C#" AutoEventWireup="True" %>
ListControl SelectedValue Example
<%@ Page Language="VB" AutoEventWireup="True" %>
ListControl SelectedValue Example
Remarks
This property returns the Value property of the selected ListItem. The 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 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 ThemeableAttribute and ASP.NET Themes and Skins.
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:
Me.DropDownList1.Items.Add(New ListItem( Text="Hello", Value="1" ))
If DropDownList1.Items.FindByValue("2") IsNot Nothing Then
Response.Write("Found")
End If
this.DropDownList1.Items.Add(new ListItem{ Text="Hello", Value="1" });
if(DropDownList1.Items.FindByValue("2") != null) {
Response.Write("Found");
}