I have a user control that contains only a text box and on another form I add this user control dynamically, a user can add the user control many times. I use a session variable to recreate the user control (maybe this approach doesn't sound cool). After recreating the control the value of the textbox disappears, obviously. Is there any solution to maintain the state of the user control on postback?
-
[possible dupe](http://stackoverflow.com/questions/113392/dynamically-added-controls-in-asp-net) – Wim Ombelets Jan 08 '13 at 13:44
-
The view state is the preferred method. – Simon Mourier Jan 08 '13 at 14:36
4 Answers
If you add dynamic controls back to the control during the correct Page Life Cycle event(PreInit
) they will maintain their state through the IPostBackDataHandler
interface.
PreInit - Create or re-create dynamic controls.

- 21,012
- 5
- 52
- 81
-
This seems like good advice; however, all the controls are still `null` at this stage. So how can you add a dynamic control to, for example, a `Placeholder` control that is null? – Jonathan Wood Jun 03 '16 at 03:21
I've had the same problem in the past.
What I did was give the dynamically-added control an ID, and made sure it retained that ID also on postback (in my case, I kept all the information in the session, and re-created the controls).
Once the postbacked control has the same ID as as before, Microsoft did magic and refilled the controls with the pre-postback values.

- 8,460
- 5
- 44
- 66
-
I know this is kind of a old post. But it would be nice if you could help me with an example. I have almost similar question here. http://stackoverflow.com/questions/40058699/how-to-re-use-already-bound-data-in-a-user-control-on-post-back I am able to store it in session, but I am not able to get it to bind. – thebenman Oct 27 '16 at 11:47
Use the object cache. Add the usercontrol into the cache and retrieve it when you need it.
You can see a nice example of how this works at: ASP.net-Tutorials Cache and Object Cache.
I am also learning asp.net now and found that quite a nice explanation. I also used the Microsoft Library

- 221
- 1
- 8
-
7Don't do this :-) The cache should never be used as a primary storage, only for something you can put back in if you need to. Cache can just go away if the ASP.NET process decides to recycle for some reason, while the session and view state will survive. – Simon Mourier Jan 08 '13 at 14:35
Every server control that inherits the IPostBackDataHandler interface has a LoadPostData method that processes the postback data. When control is implemented by a class (page, form, placeholders, controls etc), that class calls the LoadPostData method and passes the posted data and key to maintain control states.
All you need to do is to re-instantiate / reinitialize dynamic controls before or within page load event each and every time during postback and add this control to page / forms / placeholders. Then the posted data will automatically be assigned to the control by calling LoadPostData method by the parent control.
Check this article to learn how to write code for dynamic control - How to maintain dynamic control events, data during postback in asp.net

- 616
- 6
- 17

- 5,025
- 2
- 18
- 9
-
Please fix your grammar; you had no capitalization, no punctuation, and terrible sentence syntax. Your answer is useless if no one can understand it. – ragingasiancoder Jun 30 '16 at 16:40