Events in my dynamically loaded user controls do not fire in ASP.NET Web Application

前端 未结 2 1590
时光说笑
时光说笑 2021-01-24 12:58

I have ASP.NET Web Application that contains MasterPage, ASPX Page where I load User Controls dynamically into UpdatePanel containing PlaceHolder based on menu selection. This w

2条回答
  •  渐次进展
    2021-01-24 13:35

    You need to re-create dynamic control on every single postback, remember the Page instance is created per request, if you do not re-create the control then it wont exist on PostBack.

    Of course your control disappears, you didnt re-create it on the postback.

    see here

    Extract:

    Dynamically added controls must be programmatically added to the Web page on each and every page visit. The best time to add these controls is during the initialization stage of the page life cycle, which occurs before the load view state stage. That is, we want to have the control hierarchy complete before the load view state stage arrives. For this reason, it is best to create an event handler for the Page class's Init event in your code-behind class, and add your dynamic controls there.

提交回复
热议问题