Maintain the state of dynamically added user control on postback?

前端 未结 4 2253
醉酒成梦
醉酒成梦 2020-12-06 20:31

I have a user control that contains only a text box and on another form I add this user control dynamically, a user can add the user control many times. I use a session vari

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 21:35

    Every server control that inherits the IPostBackDataHandler interface has a LoadPostData method that processes the postback data. When control is implemented by a class (page, form, placeholders, controls etc), that class calls the LoadPostData method and passes the posted data and key to maintain control states.

    All you need to do is to re-instantiate / reinitialize dynamic controls before or within page load event each and every time during postback and add this control to page / forms / placeholders. Then the posted data will automatically be assigned to the control by calling LoadPostData method by the parent control.

    Check this article to learn how to write code for dynamic control - How to maintain dynamic control events, data during postback in asp.net

提交回复
热议问题