Supriya Srivastava Supriya Srivastava - 1 year ago 100
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?

Answer Source

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>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download