Greg
Greg

Reputation: 7383

How to use javascript with an asp.net dropdownlist control?

I don't currently use ajax.net though I would be open to it if it is the only solution. I have a auto-complete control on screen that I am using to populate a asp.net dropdownlist with values through javascript (jQuery). I have had to use EnableEventValidation="false" to allow this. After I add my options to the select and the form is posted back I would like to be able to get all the values for the option elements I have added to the asp.net dropdownlist through javascript.. Is there a good way to accomplish this?

Upvotes: 0

Views: 2880

Answers (2)

Jason
Jason

Reputation: 2741

You can get the selected value directly from the form like so:

string fooBar = Request.Form[SomeDropDown.UniqueID];

This will return the correct value no matter what you do to to the drop down options. I use javascript to change the quantity dropdown for a product based on size selection for reflecting product availability.

Upvotes: 1

hollystyles
hollystyles

Reputation: 5039

If a DropDownList leaves the server with no options, it's recreated server-side with no options (from the viewstate)

You could add the options to a hidden html control as a delimited string in your javascript as well as to the select list. Then iterate that into the control once server-side on post-back. Otherwise you could ajax them to the server and re-render the DropDownList only for each addition.

Upvotes: 3

Related Questions