sQuijeW
sQuijeW

Reputation: 194

How to keep options in listbox after a postback?

I have (1) dropdown list, (2) listboxes, (1) button on a page.

The listboxes start out empty, but it is filled via jquery/ajax on the dropdown list change.

When i click the button it causes a postback and every time it does the contents (options) within the listboxes are removed.

Questions: 1) WHY does this happen? 2) HOW do i keep the contents of the listbox from being removed? No options in the listboxes are selected

Upvotes: 0

Views: 2333

Answers (2)

ryudice
ryudice

Reputation: 37406

This happends because since you add the values with AJAX, they are not added to the ViewState, which is what asp.net uses to persist the state of the form. Your best option is probably to use AJAX to avoid a postback or load the values on your page load event only if it's a postback.

Upvotes: 2

Adil
Adil

Reputation: 148150

The content/state of form controls are stored in view state that is created on server side by asp.net engine. The elements added on client wont be accessible on server side. You can put the content data in the hidden field in javascript and get the data back on server side from the hidden field and assign to control like listbox, dropdown etc. This is how asp.net also works.

Upvotes: 4

Related Questions