ListControl.SelectedValue Proprietà

Definizione

Ottiene il valore dell'elemento selezionato nell'elenco o seleziona l'elemento nell'elenco che contiene il valore specificato.

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

Valore della proprietà

Valore dell'elemento selezionato nell'elenco. Il valore predefinito è una stringa vuota ("").

Attributi

Eccezioni

Il valore selezionato non si trova nell'elenco dei valori disponibili e lo stato di visualizzazione o l'altro stato è stato caricato (è stato eseguito un postback).

Esempio

Nell'esempio seguente viene illustrato come usare la SelectedValue proprietà per selezionare un elemento in un ListBox controllo. Si noti che questa proprietà può essere usata anche per recuperare il valore dell'elemento selezionato.

Importante

L'esempio include una casella di testo che accetta l'input dell'utente e rappresenta quindi una potenziale minaccia alla sicurezza. Per impostazione predefinita, le pagine Web ASP.NET verificano che l'input dell'utente non includa script o elementi HTML. Per altre informazioni, vedere Cenni preliminari sugli attacchi tramite script.


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

Commenti

Questa proprietà restituisce la Value proprietà dell'oggetto selezionato ListItem. La SelectedValue proprietà viene comunemente usata per determinare il valore dell'elemento selezionato nel controllo elenco. Se vengono selezionati più elementi, viene restituito il valore dell'elemento selezionato con l'indice più basso. Se non viene selezionato alcun elemento, viene restituita una stringa vuota ("").

La SelectedValue proprietà può essere usata anche per selezionare un elemento nel controllo elenco impostandolo con il valore dell'elemento.

Questa proprietà non può essere impostata da temi oppure temi di fogli di stile. Per altre informazioni, vedere ThemeableAttribute e ASP.NET Temi e skin.

Quando il valore selezionato non è incluso nell'elenco dei valori disponibili e viene eseguito un postback, viene generata un'eccezione ArgumentOutOfRangeException . Nell'esempio seguente viene illustrato come rilevare un valore non valido prima che si verifichi il postback:

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

Si applica a

Vedi anche