Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain the state of dynamically added user control on postback?

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?

like image 616
Mogli Avatar asked Dec 20 '22 11:12

Mogli


2 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.

like image 149
rick schott Avatar answered Dec 23 '22 01:12

rick schott


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.

like image 20
Yehuda Shapira Avatar answered Dec 22 '22 23:12

Yehuda Shapira