Urbi
Urbi

Reputation: 125

how to get dropdown selecteditem in server side when I have set options using javascript

I have set options on dropdownlist using javascript and cant able to get selectedItem. Any idea?

Upvotes: 0

Views: 826

Answers (2)

hollystyles
hollystyles

Reputation: 5039

ASP.NET DropDownList Server Control keeps it's options in the viewstate, when you add options client-side they will not be in the viewsate when you post-back server-side.

So you can either:

  1. do a full postback to add options server-side (Best way is put the dropdownlist in an update panel so at least the full post-back goes via Ajax)

  2. Use javascript to put the selected optoin into a hidden textbox and read that when you get server-side

  3. Or you may be able to read the raw form data in Request.Form["dropdownlistid"] but it's been a while so I'm not certain of this one.

The other common noob mistake is you are re-binding the dropdownlist in Page_Load or some other event that occurrs before the DropDownList's SelectedIndexChanged event which will remove it's selected item. This is why you need to show us more information in your question, otherwise, as you can see we are all stabbing in the dark.

Upvotes: 1

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16134

Have you removed Enabled="false" after doing a selection in Javascript, if not remove this. Otherwise, value will not post.

Using jQuery:

$("#DropdownlistID").prop('disabled', false);

Using javascript:

document.getElementById("DropdownlistID").removeAttribute("disabled");

Upvotes: 0

Related Questions