简体   繁体   中英

ASP.NET - Dynamic Control Values & Postback

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM