asp.net - 当 FormView 设置为插入模式时,控件在回发后不保留值

标签 asp.net formview

我正在使用 Page_Load 事件中的 ChangeMode 方法将 FormView 的 CurrentMode 设置为插入模式,如下所示:

if(!Page.IsPostBack)
{
    MyFormView.ChangeMode(FormViewMode.Insert);
}

在我的 FormView 的插入模板中,我有一个 DropDownList 控件,它的 AutoPostBack 属性设置为 true。我在插入模板中还有其他几个 DropDownList 和 TextBox 控件。

每当我更改 DropDownList 的选择并发生回发时,我都会丢失输入控件的所有值。奇怪的是,如果我在初始页面加载后随时使用 ChangeMode 将 FormView 设置为插入模式,我就没有问题。我已经使用调试器逐步完成了代码,一切似乎都在正确发生,但是在我的 DropDownList 事件处理程序运行后的某个时候,一切似乎都被重置了。

这里发生了什么?

更新:
我注意到我的 FormView 位于带有 runat="server"和 enableviewstate="false"的 div 标签内。为容器 div 启用 viewstate 后,我开始看到略有不同的行为。 FormView 在第一次回发后仍然不保留值,但现在后续回发工作正常并且值被保留。

任何想法将不胜感激。

最佳答案

Walter Wang [MSFT] 来自其他论坛的回答 - 2007 年 1 月 26 日 03:29 GMT

First, the problem appears to be that the FormView is told by the data source control that the data has changed, and therefore it should rebind to get the new data. This actually should not have been done since we're still in Insert mode. I have got a workaround for your reference: Inherit from FormView to create your own FormView control, override OnDataSourceViewChanged and set RequiresDataBinding to false if we're in Insert mode:

public class MyFormView : FormView
   {
       protected override void OnDataSourceViewChanged(object sender,
EventArgs e)
       {
           if (this.CurrentMode == FormViewMode.Insert)
           {
               this.RequiresDataBinding = false;
           }
           else
           {
               base.OnDataSourceViewChanged(sender, e);
           }
       }
   }


我已经在我身边测试过它,它似乎工作正常。请给它一个
试着让我知道结果。

关于asp.net - 当 FormView 设置为插入模式时,控件在回发后不保留值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/753318/

相关文章:

asp.net - 如何在运行时重写 Web Config 连接字符串

c# - 无法在 Visual Studio 2008 的代码隐藏中引用 ASP.NET 控件

c# - 从后面的代码访问 formView 页脚模板内的 userControl

c# - 在表单 View 中设置当前日期

asp.net - FormView 不显示

asp.net - Chrome 网络选项卡中的延迟测量

asp.net - 找不到 ASP.net 用户控件的命名空间名称

javascript - 在 ASP.NET Web 应用程序中包含 Google Analytics javascript 代码

c# - 将 FormView 中的列表绑定(bind)到 ASP.net webforms 中的模型

asp.net - 内部 .ascx 控件和 FormView 中的数据绑定(bind)