Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Populating DropDownList w/ Clientside Javascript

I have a javascript function that dynamically adds options to a select box.  However, when I do a PostBack, the dropdownlist control keeps the old list of items (probably due to Viewstate?).

Is there any way I can add items to a DropDownList without doing a postback?  I need to be able to see all the items, not just the one that was selected.

Monday, June 23, 2003

Correct. When you post back, the page is rebuilt from scratch and when the DropDownList control is recreated, the page reads its old values out of viewstate and repopulates the list.

Speaking practically, you can't mess with viewstate from client code. However, what you could do is go ahead and add items to the dropdown and also add the same information to a hidden field that has an id attribute and runat=server. When the page is posted, in the Page_Load handler you could get the values ouf of the hidden field and add them to the drop-down list server-side.

Make sense? IOW, use your own hidden field to share information between client script and server code.

-- Mike

mike pope
Wednesday, July 2, 2003

*  Recent Topics

*  Fog Creek Home