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