HtmlSelect.Value Property
Gets the value of the selected item in the HtmlSelect control or sets the SelectedIndex property of the control to the index of the first item in the list with the specified value.
[Visual Basic] Public Property Value As String [C#] public string Value {get; set;} [C++] public: __property String* get_Value(); public: __property void set_Value(String*); [JScript] public function get Value() : String; public function set Value(String);
Property Value
The value of the selected item in the HtmlSelect control. If no item is selected in the control, String.Empty is returned.
Remarks
The Value property is commonly used to get the value of the item referenced by the SelectedIndex property.
Note If no item is selected in the control, String.Empty is returned.
If you set this property to a value, the SelectedIndex property is updated with the index of the first item in the list that contains that value. If no matching value is found, the SelectedIndex property is left unchanged.
Example
[Visual Basic, C#, JScript] The following example demonstrates how to use the Value property to get the value of the selected item in the HtmlSelect control.
[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <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 runat="server"> <h3> HtmlSelect Example </h3> Select items from the list: <br><br> <select id="Select1" Multiple="False" runat="server"> <option value="Text for Item 1" Selected="True"> 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> [C#] <%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <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 runat="server"> <h3> HtmlSelect Example </h3> Select items from the list: <br><br> <select id="Select1" Multiple="False" runat="server"> <option value="Text for Item 1" Selected="True"> 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> [JScript] <%@ Page Language="JScript" AutoEventWireup="True" %> <html> <head> <script runat="server"> function Button_Click (sender : Object, e: EventArgs) : void { Label1.Text = "You selected the item with index number " + Select1.SelectedIndex.ToString() + " and contains the value " + Select1.Value + "."; } </script> </head> <body> <form runat="server"> <h3> HtmlSelect Example </h3> Select items from the list: <br><br> <select id="Select1" Multiple="False" runat="server"> <option value="Text for Item 1" Selected="True"> 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>
[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
See Also
HtmlSelect Class | HtmlSelect Members | System.Web.UI.HtmlControls Namespace | SelectedIndex | String.Empty