Supriya Srivastava Supriya Srivastava - 3 months ago 16x
Javascript Question

Dropdownlist that is populated by javascript gets reset after postback

I am using js function to add items in dropdownlist.All items are added perfectly but when I want to add selected value from that in DB,On clicking button the dropdownlist gets reset on postback.

How can I stop this?


The items are lost on postback because they are added client side through javascript, and do not exist in viewstate.

An option would be to add an <asp:hiddenfield />, and add some javascript code to the onChange event of your dropdown to write the selectedvalue into the hiddenfield input. This value will then be accesible after postback. You still have to re-populate the dropdown again after postback though.


<asp:HiddenField ID="DropDownValue" runat="server" />
<select onChange='document.getElementById("<%= DropDownValue.ClientID %>").value = this.value;'></select>