views:

661

answers:

3

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?

+3  A: 

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
Thank you very much.
HasanGursoy
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
Any Luck with multiple values selected by user ?!
jalchr
@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
+1  A: 

Java script:

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

pavun_cool
+2  A: 

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

Hope it helps.

EDIT, you'd need to specify a name of the select not only ID to be able to Request.Form["testSelect"]

Pontus Bremdahl