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

前端 未结 2 603
醉酒成梦
醉酒成梦 2021-01-16 10:13

I have a user control which contains several buttons, depending on the button pressed a different control is added to the page (lets say button 1 adds a TextBox, button2 add

相关标签:
2条回答
  • 2021-01-16 10:28

    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.

    0 讨论(0)
  • 2021-01-16 10:30

    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.

    0 讨论(0)
提交回复
热议问题