ListControl.SelectedValue 属性

定义

获取列表控件中选定项的值,或选择列表控件中包含指定值的项。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.Browsable(false)]
[System.ComponentModel.Bindable(true)]
public virtual string SelectedValue { get; set; }
[System.ComponentModel.Browsable(false)]
[System.Web.UI.Themeable(false)]
[System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)]
public virtual string SelectedValue { get; set; }
member this.SelectedValue : string with get, set
Public Overridable Property SelectedValue As String

属性值

列表控件中选定项的值。The value of the selected item in the list control. 默认值为空字符串("")。The default is an empty string ("").

属性

例外

选定值是不在可用值列表中,视图状态或其他状态已加载(已执行回发)。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).

示例

下面的示例演示如何使用 SelectedValue 属性选择 ListBox 控件中的项。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.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > 

<head runat="server">
    <title> ListControl SelectedValue Example </title>
<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 id="form1" 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>

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > 

<head runat="server">
    <title> ListControl SelectedValue Example </title>
<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 id="form1" 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>

注解

此属性返回所选 ListItemValue 属性。This property returns the Value property of the selected ListItem. SelectedValue 属性通常用于确定列表控件中选定项的值。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.

SelectedValue 属性还可用于在列表控件中选择项,方法是将其设置为项的值。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. 有关详细信息,请参阅 ThemeableAttributeASP.NET 主题和外观For more information, see ThemeableAttribute and ASP.NET Themes and Skins.

如果所选的值不在可用值列表中,并且执行了回发,则会引发 ArgumentOutOfRangeException 异常。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");
}

适用于

另请参阅