views:

438

answers:

3

using c# (asp.net)

i'm programmatically creating several drop down lists (random number). the user selects a particular value from each list and i need to save the user selected value.

if i try to save the data to session state by using a button click event it says the drop down list object hasn't been created. (obviously cuz i'm creating the drop down lists in the page load event under !IsPostBack.)

if i try to save the data to session state in the page load event under IsPostBack i only get the first value from each list. (obviously cuz when the page is recreated after the postback, the drop down lists have been recreated and the user entered data is lost).

How do i save the user selected value from the drop down lists when a button is clicked? i'm doing this in the code behind file.

A: 

You have to recreate all of the dropdowns on every postback.

John Saunders
A: 

Personally (without the ability to switch to MVC) I would consider getting rid of the postbacks altogether and reading the values back directly from a standard HTTP POST request.

The implementation of dynamic controls with postbacks often ends up rather convoluted and difficult to follow

AdamRalph
true. i'm now having another detailed read of the page life cycle. still new to online dev so it'll be a while before i get to MVC and http posts.
see_sharp_guy
If you're new to online dev then I would really recommend learning MVC rather than Web Forms.
AdamRalph
A: 

As John Saunders said, you have to recreate all of your DropDownList controls on every postback. Additionally, you have to create them before state is restored. That means that "under !IsPostBack" in the page load event is already too late. Move it up to Page Init.

Joel Coehoorn
ahhh, ok. i'll create the controls in page init. that way when the button click event fires, an instance of each drop down list object WILL exist.in effect i guess that uses a total of two page cycles.
see_sharp_guy