36

I have code below:

<select id="testSelect">
    <option value="1">One</option>
    <option value="2">Two</option>
</select>
<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?

HasanG
  • 12,734
  • 29
  • 100
  • 154

5 Answers5

47

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:

Request.Form["testSelect"]
Kobi
  • 135,331
  • 41
  • 252
  • 292
  • 1
    No problem. Pontus makes a valid point about using a server-side Drop-Down, if possible, but I assumed you had a good reason not to use it. – Kobi Mar 04 '10 at 10:19
  • Any Luck with multiple values selected by user ?! – Jalal El-Shaer Apr 02 '10 at 16:47
  • 9
    @jalchr - For multiple values you can use `Request.Form.GetValues("testSelect")`. In the future, you can probably ask a new question, it will get you a faster answer. – Kobi Apr 02 '10 at 17:26
  • That doesn't work for me; if interested, see Update 3 here: http://stackoverflow.com/questions/42655000/how-can-i-get-the-text-of-the-selected-item-in-an-html-select-element – B. Clay Shannon-B. Crow Raven Apr 07 '17 at 20:19
10

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

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

testSelect.SelectedIndex == 0 ? "uninformed" : 
    testSelect.Items[testSelect.SelectedIndex].Text);

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

HasanG
  • 12,734
  • 29
  • 100
  • 154
8

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

Cullub
  • 2,901
  • 3
  • 30
  • 47
2
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html>
<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 = "Selected index: " + Select1.SelectedIndex.ToString()
                       + ", value: " + Select1.Value;    
      }    
   </script>    
</head>    
<body>    
   <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>
      </select>

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

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

Source from Microsoft. Hope this is helpful!

HasanG
  • 12,734
  • 29
  • 100
  • 154
QuanCSE
  • 29
  • 1
1

Java script:

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

Thomas G
  • 9,886
  • 7
  • 28
  • 41
Pavunkumar
  • 5,147
  • 14
  • 43
  • 69