ASP.NET Add Control on postback

后端 未结 1 828
长情又很酷
长情又很酷 2021-01-21 17:08

I\'ve put together a simple form to highlight the concepts of dynamic forms. What I need to do is add a control to the page when the user clicks the \"Add\" button.

I h

1条回答
  •  醉话见心
    2021-01-21 17:51

    This does create the new controls based on the incremented value, but all the control state is lost.

    You're calling the function too late in the page life cycle. State is applied to your controls for the "Load" stage, and so if the controls are not created before that stage the state won't be restored, because the controls don't exist when it tries to apply the state.

    You need to create the controls in the Page's Init event.

    Personally, I'm not a fan of dynamic controls in ASP.Net. They have their place, but more often I choose a suitable maximum number of allowed controls, put them all on the page initially, and only enable/disable/hide/show them as needed.

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