Home » Html » How to get selected value of a html select with asp.net

How to get selected value of a html select with asp.net

Posted by: admin November 30, 2017 Leave a comment


I have code below:

<select id="testSelect">
    <option value="1">One</option>
    <option value="2">Two</option>
<asp:Button ID="btnTest" runat="server" Text="Test it!" onclick="btnTest_Click" />

I need to get selected options’ value on postback. How can I do this with asp.net?


You need to add a name to your <select> element:

<select id="testSelect" name="testSelect">

It will be posted to the server, and you can see it using:



If you would use asp:dropdownlist you could select it easier by testSelect.Text.

Now you’d have to do a Request.Form["testSelect"] to get the value after pressed btnTes.

Hope it helps.

EDIT: You need to specify a name of the select (not only ID) to be able to Request.Form["testSelect"]


I’ve used this solution to get what you need.

Let’say that in my .aspx code there’s a select list runat=”server”:

<select id="testSelect"  runat="server" ClientIDMode="Static" required>
    <option value="1">One</option>
    <option value="2">Two</option>

In my C# code I used the code below to retrieve the text and also value of the options:

testSelect.SelectedIndex == 0 ? "uninformed" : 

In this case I check if the user selected any of the options. If there’s nothing selected I show the text as “uninformed”.


Java script:

use elementid. selectedIndex() function to get the selected index

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">    
    <title> HtmlSelect Example </title>
    <script runat="server">
      void Button_Click (Object sender, EventArgs e)
         Label1.Text = "Selected index: " + Select1.SelectedIndex.ToString()
                       + ", value: " + Select1.Value;    
   <form id="form1" runat="server">

      Select an item: 

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

      <button onserverclick="Button_Click" runat="server" Text="Submit"/>

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

Source from Microsoft. Hope this is helpful!