views:

61

answers:

1

I'm trying to persist the contents of a textbox through a postback, and I've exhausted all of my effort but can't get it working right.

What should happen is:

  1. User selects a radiobutton
  2. Depending which button was selection, a usercontrol is loaded to specify some data and a viewstate to say which enum type it's equivalent to.
  3. When they click save, if the UserControl is just a textbox input - the simplest), the contents are read and saved, then saved to the database with the format(the radiobutton choice) so they can be deserialized again later.
  4. The page posts back, and the value and format are read from the database, then the right control is loaded.

The problem is - the first time the page posts back, it works. Every other postback it resets to the default value of the textbox.

I have a very similar setup elsewhere, so I'm thinking it might be a minor thing I'd never think of. There's a lot of code, so it might be easier to talk about what to do (load the dynamic control, populate the values etc) rather than how to do it.

A: 

There was actually a bug in my original code which meant it would never have in the way I was using the modified version. Apparently state is restored in Page_Load, so any controls need to be initialized by to have their values restored.

Creating the control in Page_PreLoad, then populating it after Page_Load solved the problem.

Echilon