HtmlSelect.Value Propiedad

Definición

Obtiene el valor del elemento seleccionado en el control HtmlSelect o establece la propiedad SelectedIndex del control en el índice del primer elemento de la lista que tenga el valor especificado.

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

Valor de propiedad

Valor del elemento seleccionado del control HtmlSelect. Si no hay ningún elemento seleccionado en el control, se devuelve Empty.

Excepciones

La propiedad Value se ha establecido en un valor mayor que el número de elementos en el control HtmlSelect o menor que -1.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la Value propiedad para obtener el valor del elemento seleccionado en el HtmlSelect control .


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

Comentarios

La Value propiedad se usa normalmente para obtener el valor del elemento al que hace referencia la SelectedIndex propiedad .

Nota

Si no hay ningún elemento seleccionado en el control, se devuelve String.Empty.

Si establece esta propiedad en un valor, la SelectedIndex propiedad se actualiza con el índice del primer elemento de la lista que contiene ese valor. Si no se encuentra ningún valor coincidente, la SelectedIndex propiedad se deja sin cambios.

Se aplica a

Consulte también