ListControl.SelectedValue 属性

定义

获取列表控件中选定项的值,或选择列表控件中包含指定值的项。

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; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedValue : string with get, set
[<System.ComponentModel.Browsable(false)>]
[<System.ComponentModel.Bindable(true, System.ComponentModel.BindingDirection.TwoWay)>]
[<System.Web.UI.Themeable(false)>]
member this.SelectedValue : string with get, set
Public Overridable Property SelectedValue As String

属性值

String

列表控件中选定项的值。 默认值为空字符串("")。

属性

例外

选定值是不在可用值列表中,视图状态或其他状态已加载(已执行回发)。

示例

以下示例演示如何使用 SelectedValue 属性选择控件中的 ListBox 项。 请注意,此属性还可用于检索所选项的值。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述


<%@ 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"/>

        

      <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"/>

        

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

      <br /><br />

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

   </form>

</body>
</html>

注解

此属性返回 Value 所选 ListItem属性。 该 SelectedValue 属性通常用于确定列表控件中所选项的值。 如果选择了多个项,则返回具有最低索引的选定项的值。 如果未选择任何项,则返回空字符串 (“”) 。

SelectedValue 属性还可用于通过设置项的值来选择列表控件中的项。

无法通过主题或样式表主题设置此属性。 有关详细信息,请参阅ThemeableAttributeASP.NET 主题和外观

当所选值不在可用值列表中并且执行回发时, ArgumentOutOfRangeException 将引发异常。 以下示例演示如何在回发前捕获无效值:

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");
}

适用于

另请参阅