且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET 在回发时添加控件

更新时间:2023-12-06 10:41:52

这确实会根据增加的值创建新控件,但所有控件状态都会丢失.

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.

您需要在 Page 的 Init 事件中创建控件.

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

就我个人而言,我不喜欢 ASP.Net 中的动态控件.它们有自己的位置,但更多时候我会选择一个合适的最大允许控件数量,最初将它们全部放在页面上,然后仅根据需要启用/禁用/隐藏/显示它们.

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.