Dynamic Controls and Postback

六月ゝ 毕业季﹏ 提交于 2019-12-29 08:25:51

问题


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


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/5430982/dynamic-controls-and-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!