ASP.net Dynamically adding controls on Button press. Postback issue

放肆的年华 提交于 2019-12-01 13:24:05

Because you are doing this dynamically, you need a way of storing what your are doing so the server can recreate it with each PostBack. If you don't want to use Session, store the data in the ViewState (which will persist with the page regardless of time). Create a List<YourControlObjects> and make sure it is Serializable and then store it in the ViewState. You probably want to store the control type, location, etc. so that you can rebuild it on Page_Load each time there is a PostBack.

The problem comes down to you needing to maintain your own state for these dynamically created controls. This is just one suggestion but you could do this many different ways.

I personally would handle this using a ListView. Include all of the controls you need in the ItemTemplate with Visible=false and bind them to a small list stored in the viewstate. Programatically set the correct control visible on row databind. Note you will have to collect your data in the controls and save it in your list before you rebind it.

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