views:

1568

answers:

2

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?

+3  A: 

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.

HollyStyles
I was trying to avoid using a hidden field, but if that is the only way..
Greg
I've tried a bunch of things, and without using ASP.NET's "Ajax" library, I think HollyStyles' solution is the only one available.I feel your pain though.
roosteronacid
+1  A: 

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.

Jason
This really works. Thanks!
Alexander Prokofyev