gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:03:17 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com