Event won't fire to dynamically added control

后端 未结 1 1451
鱼传尺愫
鱼传尺愫 2020-12-12 07:59

I\'m dynamically adding htmlvideo controls to my web form based on the number of videos present on the server folder. This part works fine. All the videos show up and can be

相关标签:
1条回答
  • 2020-12-12 08:26

    This is an old ViewState problem for any dynamic control, and has nothing specific to do with video.

    Remember, every PostBack rebuilds the entire page from scratch, including your dynamic controls. If you still want to see these controls after a postback (which includes all server events), you must re-add them to the page. Additionally, you need a control's ViewState restored if you want an event fired for the control during this PostBack, and for the ViewState to restore the control must be added back to the reconstructed page before the Page_Load event runs. Page_Init or Page_PreInit can work well for this.

    Finally, consider the performance implications here. Do you really want to rebuild the entire page on every user interaction, or is it perhaps time to learn to use javascript to process these things, with maybe a web api that only has to receive a javascript request without causing an entire page cycle both on your server and in the user's browser?

    Just a few of the many other times this has been asked and answered:

    • Dynamic Event Handler not Firing
    • dynamically added buttons not firing click event c#
    • dynamically created button click event not firing
    • Dynamically Added DropDownlists Are Not Firing SelectedIndexChanged Event
    • ASP.NET: Viewstate and programmatically adding user controls
    • Click events on Array of buttons
    • VB ASP dynamic button click event not hitting handler event
    0 讨论(0)
提交回复
热议问题