This documentation is archived and is not being maintained.

ListControl.SelectedValue Property

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

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

[Visual Basic]
Public Overridable Property SelectedValue As String
[C#]
public virtual string SelectedValue {get; set;}
[C++]
public: __property virtual String* get_SelectedValue();
public: __property virtual void set_SelectedValue(String*);
[JScript]
public function get SelectedValue() : String;
public function set SelectedValue(String);

Property Value

The value of the selected item in the list control. The default is an empty string ("").

Exceptions

Exception Type Condition
ArgumentOutOfRangeException The SelectedValue property is set with a value that does not correspond to an item in the list control.

Remarks

The SelectedValue property is commonly used to determine the value of the selected item in the list control. 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. If no items in the list control contain the specified value, a System.ArgumentOutOfRangeException is thrown.

Example

[Visual Basic, C#] 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.

[Visual Basic] 
<!-- The following example demonstrates how to select items in a TextBox. 
If the item is found in the text box, the item is selected and a message is 
displayed stating the name of the selected item. If the item is not found, 
no item is selected and a message is displayed stating that the item was not 
found. -->

<%@ Page Language="VB" AutoEventWireup="True" %>

<html> 

<head>

   <script runat="server">

      Sub Button_Click(sender As Object, e As EventArgs)

         ' Perform this operation in a try-catch block in case the item is not found.
         Try
   
            List.SelectedValue = ItemTextBox.Text
            MessageLabel.Text = "You selected " & List.SelectedValue + "."
        
         Catch ex As Exception
     
            List.SelectedValue = Nothing         
            MessageLabel.Text = "Item not found in ListBox control."
     
         End Try
             
      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3> ListControl SelectedValue Example </h3>
 
      <asp:ListBox ID="List"
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>

      </asp:ListBox>

      <hr>

      Enter the value of the item to select: <br>
      <asp:TextBox ID="ItemTextBox"
           MaxLength="6"
           Text="Item 1"
           runat="server"/>

      &nbsp;&nbsp;

      <asp:Button ID="SelectButton"
           Text="Select Item"
           OnClick="Button_Click"
           runat="server"/>

      <br><br>

      <asp:Label ID="MessageLabel"
           runat="server"/>     

   </form>

</body>
</html>

[C#] 
<!-- The following example demonstrates how to select items in a TextBox. 
If the item is found in the text box, the item is selected and a message is 
displayed stating the name of the selected item. If the item is not found, 
no item is selected and a message is displayed stating that the item was not 
found. -->

<%@ Page Language="C#" AutoEventWireup="True" %>

<html> 

<head>

   <script runat="server">

      void Button_Click(Object sender, EventArgs e)
      {

         // Perform this operation in a try-catch block in case the item is not found.
         try
         {
            List.SelectedValue = ItemTextBox.Text;
            MessageLabel.Text = "You selected " + List.SelectedValue + ".";
         }
         catch (Exception ex)
         {
            List.SelectedValue = null;
            MessageLabel.Text = "Item not found in ListBox control.";
         }
             
      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> ListControl SelectedValue Example </h3>
 
      <asp:ListBox ID="List"
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>

      </asp:ListBox>

      <hr>

      Enter the value of the item to select: <br>
      <asp:TextBox ID="ItemTextBox"
           MaxLength="6"
           Text="Item 1"
           runat="server"/>

      &nbsp;&nbsp;

      <asp:Button ID="SelectButton"
           Text="Select Item"
           OnClick="Button_Click"
           runat="server"/>

      <br><br>

      <asp:Label ID="MessageLabel"
           runat="server"/>     

   </form>

</body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

ListControl Class | ListControl Members | System.Web.UI.WebControls Namespace | SelectedItem | SelectedIndex | DataValueField | Items | ListItem | System.ArgumentOutOfRangeException

Show: