Link to home
Start Free TrialLog in
Avatar of foltz
foltz

asked on

ASP.NET ViewState and dynamically created controls

Hi,

I am experiencing a problem with ViewState on re-post when I create controls dynamically. I cannot get the value of the dynamically created controls on the post back. As I understand, I have to recreate the controls in Page_Init event so that I can retrieve the values. BUT my problem is I don’t know the control names.

I create textboxes dynamically and name them as ‘txtStaff’ and staff_id. For instance ‘txtStaff12’ where staff_id is 12. My problem is I cannot recreate the textboxes in Page_Init as I don’t know the control names. I also am not able to access any values in ViewState. Can you pls. let me know a way to get around this?

Thank you for any help.
Avatar of Cronan
Cronan

Where are you currently creating the controls?
ASKER CERTIFIED SOLUTION
Avatar of Cronan
Cronan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For each post back, the controls will be regenerated, and so the controls you put to viewstate will be different than the one you generated after the post back. One option is to manage the control state and not the control itself. another option is to pass the control reference 'after' you create the control for post back. Here are some of the topics related to this issue.
https://www.experts-exchange.com/questions/20711120/Custom-object-in-either-Viewstate-or-session-state.html