C# 正确使用asp.net webforms生命周期
这确实是一个更一般的问题,但我能想到的唯一方法是用一个具体的例子 我们目前有一个带有SPGridView的Web部件。现在,GV和所有绑定字段在CreateChildControls中创建,数据在OnPreRender中检索和绑定。这些列是静态的,因此可以正常工作C# 正确使用asp.net webforms生命周期,c#,asp.net,.net,web-parts,C#,Asp.net,.net,Web Parts,这确实是一个更一般的问题,但我能想到的唯一方法是用一个具体的例子 我们目前有一个带有SPGridView的Web部件。现在,GV和所有绑定字段在CreateChildControls中创建,数据在OnPreRender中检索和绑定。这些列是静态的,因此可以正常工作 CreateChildControls{ // create and configure the gridview // create all bound fields and add them to the grid
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中创建所有可能的绑定字段,然后在OnPreRender中将适当的字段添加到GV中。我们可以将所有绑定字段的创建完全移动到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事件中安全地添加列。BoundField不是控件,gridview中的Columns集合是状态管理的,因此您可以安全地在对控件进行数据绑定之前,在PreRender事件中添加列。可能不是一个完整的答案,但这里有一些关于ASP.NET页面生命周期的MSDN信息。另外,请注意页面底部的一些评论,以提供更多信息/评论。编辑:如果我包含以下链接,会很有帮助:可能不是完整的答案,但这里有一些关于ASP.NET页面生命周期的MSDN的浅显读物。另外,请注意页面底部的一些评论,以提供更多信息/评论。编辑:如果我包含以下链接,则帮助: