c# - 正确使用 asp.net webforms 生命周期

标签 c# asp.net .net web-parts

这确实是一个更笼统的问题,但我能想到的唯一提问方式是举一个具体的例子。

我们目前有一个带有 SPGridView 的 Web 部件。现在,GV 和所有绑定(bind)字段都在 CreateChildControls 中创建,数据在 OnPreRender 中检索和绑定(bind)。这些列是静态的,因此可以正常工作。

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 中,我们无法从下拉控件中获取值,因此我们无法有条件地添加绑定(bind)字段。我的问题是,这里的最佳做法是什么?我们可以在 CreateChildControls 中创建所有可能的绑定(bind)字段,然后仅将适当的字段添加到 OnPreRender 中的 GV。我们可以将所有绑定(bind)字段的创建完全移动到 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 事件之前安全地添加列您对控件进行数据绑定(bind)。

关于c# - 正确使用 asp.net webforms 生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11017638/

相关文章:

c# - 哪个线程最先进入临界区?

c# - 为什么多次调用 app.UseEndpoints(..) 时中间件不执行?

asp.net mvc 从 Html.textbox() 获取值

jquery - 通过单击按钮添加多个 DropDownList

c# - MVVMCross - 跨应用程序共享业务逻辑

c# - 这是一个糟糕的设计吗?

javascript - 在 JavaScript 中输出 ASP.NET 模型

c# - 使用数据绑定(bind)访问 DataTemplate 中的 WPF 组件

c# - 调用 .Save() 后亚音速日期发生变化

javascript - 强制用户在文本框输入中输入特定详细信息