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:
- User selects a radiobutton
- 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.
- 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.
- 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 de开发者_JS百科fault 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.
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.
精彩评论