正确使用 asp.net 网络表单生命周期
本文关键字:网络 表单 生命 周期 net asp | 更新日期: 2023-09-27 18:37:15
这确实是一个更普遍的问题,但我能想到的唯一方法是用一个具体的例子。
我们目前有一个带有SPGridView
的 Web 部件。 就像现在一样,GV 和所有绑定字段都是在 CreateChildControls
中创建的,数据在 OnPreRender
中检索和绑定。 列是静态的,因此工作正常。
CreateChildControls{
// create and configure the gridview
// create all bound fields and add them to the gridview
// add the gridview to the page
}
OnPreRender{
// get the data and bind it to the gridview
}
现在我们需要将列更改为依赖于用户从下拉列表中所做的选择。 在CreateChildControls
内,我们无法从下拉控件中获取值,因此无法有条件地添加绑定字段。 我的问题是,这里的最佳实践是什么? 我们可以在 CreateChildControls
中创建所有可能的绑定字段,然后只将适当的字段添加到 GV 中 OnPreRender
. 我们可以将所有绑定字段的创建完全移动到 OnPreRender
中。 而且还有很多其他选择。
CreateChildControls{
// create and configure the gridview
// create ALL bound fields here?
// add the gridview to the page
}
OnPreRender{
// or maybe create only the applicable bound fields here?
// add the appropriate fields to the gridview
// get the data and bind it to the gridview
}
在更一般的意义上,什么真正构成"创建"控件(CreateChildControls
的目的)? 这个问题实际上扩展到任何可能具有动态内容的控件。 在哪里是将条目添加到下拉列表中的合适位置,等等。 有很多方法可以工作,但哪种是"正确的"? 将选择添加到下拉列表中是"创建"控件的一部分吗? 它是否取决于选择是否动态?
> BoundField
不是控件,GridView 中的Columns
集合是状态管理的,因此您可以在数据绑定控件之前安全地在 PreRender
事件中添加列。