asp.net dynamically added user control saving values after postback

前端 未结 7 827
半阙折子戏
半阙折子戏 2021-01-05 10:27

Here\'s my issue. I have a usercontrol that I want to allow users to add as many instances of as necessary using a button click (each time a button is clicked, I want to ad

7条回答
  •  臣服心动
    2021-01-05 10:49

    If you are keeping track of the number of controls the user has added you need to recreate the controls the user added previously, preferably in Page_Init or Page_Load. Add something like this to that handler:

    for(int i=0; i

    If you do it this way, the state of the controls should be maintained because when you add the tb control to a controls collection it plays catch up with events and should automagically restore its viewstate.

    You shouldn't have to keep track of their state in session as in most cases it will be stored in the viewstate for you

提交回复
热议问题