Tarun
Tarun

Reputation: 267

Dynamic Controls and Postback

I have a Panel. Now in that panel, i'm adding controls. It is getting added and displayed.The problem is when the page is posted back.I know the controls have to be binded again in the panel. But lets say if the user has entered some value in the dynamic created text box. Its is getting lost..

Upvotes: 0

Views: 3443

Answers (2)

gdp
gdp

Reputation: 8242

I have done this before by storing the data from the controls in session.

Every time you dynamically add a control, store the current data entered into the controls in session or viewstate for example, and then rebind on postback. Not the most elegant solution but it worked. I take it this is a webforms question?

  1. Add dynamic control click
  2. Save current form data
  3. On page_load load the data from viewstate into the controls

Upvotes: 3

jaraics
jaraics

Reputation: 4299

Make sure that the controls get the same ID each time (for ex. by specifying an ID explicitly) and you should add the dynamic controls on Page.Init so they can participate in the page's life-cycle.

There's an article on 4guysfromrolla.com, Dynamic Web Controls, Postbacks, and View State

Upvotes: 2

Related Questions