HtmlSelect.Value Proprietà

Definizione

Ottiene il valore dell'elemento selezionato nel controllo HtmlSelect oppure imposta la proprietà SelectedIndex del controllo sull'indice del primo elemento dell'elenco avente il valore specificato.

public:
 property System::String ^ Value { System::String ^ get(); void set(System::String ^ value); };
public string Value { get; set; }
member this.Value : string with get, set
Public Property Value As String

Valore della proprietà

Valore dell'elemento selezionato nel controllo HtmlSelect. Se nel controllo non è selezionato alcun elemento, viene restituito il valore Empty.

Eccezioni

La proprietà Value è stata impostata su un valore maggiore del numero di elementi presenti nel controllo HtmlSelect o minore di -1.

Esempio

Nell'esempio di codice seguente viene illustrato come usare la Value proprietà per ottenere il valore dell'elemento selezionato nel HtmlSelect controllo.


<%@ 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>
    <title> HtmlSelect Example </title>
<script runat="server">

      void Button_Click (Object sender, EventArgs e)
      {
        
         Label1.Text = "You selected the item with index number " + 
                       Select1.SelectedIndex.ToString() + 
                       " and contains the value " +
                       Select1.Value + ".";

      }

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list: <br /><br />

      <select id="Select1" 
              runat="server">

         <option value="Text for Item 1" selected="selected"> Item 1 </option>
         <option value="Text for Item 2"> Item 2 </option>
         <option value="Text for Item 3"> Item 3 </option>
         <option value="Text for Item 4"> Item 4 </option>
         <option value="Text for Item 5"> Item 5 </option>
         <option value="Text for Item 6"> Item 6 </option>

      </select>

      <br /><br />

      <button id="Button1"
              onserverclick="Button_Click"
              runat="server">

         Submit

      </button>

      <br /><br />

      <asp:Label id="Label1"
           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>
    <title> HtmlSelect Example </title>
<script runat="server">

      Sub Button_Click (sender As Object, e As EventArgs)
        
         Label1.Text = "You selected the item with index number " & _
                       Select1.SelectedIndex.ToString() & _
                       " and contains the value " & _
                       Select1.Value & "."

      End Sub

   </script>

</head>

<body>

   <form id="form1" runat="server">

      <h3> HtmlSelect Example </h3>

      Select items from the list: <br /><br />

      <select id="Select1" 
              runat="server">

         <option value="Text for Item 1" selected="selected"> Item 1 </option>
         <option value="Text for Item 2"> Item 2 </option>
         <option value="Text for Item 3"> Item 3 </option>
         <option value="Text for Item 4"> Item 4 </option>
         <option value="Text for Item 5"> Item 5 </option>
         <option value="Text for Item 6"> Item 6 </option>

      </select>

      <br /><br />

      <button id="Button1"
              onserverclick="Button_Click"
              runat="server">

         Submit

      </button>

      <br /><br />

      <asp:Label id="Label1"
           runat="server"/>

   </form>

</body>

</html>

Commenti

La Value proprietà viene comunemente usata per ottenere il valore dell'elemento a cui fa riferimento la SelectedIndex proprietà .

Nota

Se nel controllo non è selezionato alcun elemento, viene restituito il valore String.Empty.

Se si imposta questa proprietà su un valore, la SelectedIndex proprietà viene aggiornata con l'indice del primo elemento nell'elenco contenente tale valore. Se non viene trovato alcun valore corrispondente, la SelectedIndex proprietà viene lasciata invariata.

Si applica a

Vedi anche