0

我正在使用 FormView 控件,并且我想通过 FormViewUpdateEventArgs 参数中的 NewValues 字典以编程方式在 OnItemInserting 和 OnItemUpdating 事件中插入空值。但是如果我使用

protected void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    e.NewValues["EntityFrameworkEntityPropertyName"] = null;
}

此类修改被忽略,数据库列不更新并保持其旧值。EntityFramework 实体属性为:Type Int32、Nullable True、StoreGeneratedPattern None。

如果我通过 Bind() 将 TextBox 控件直接绑定到属性,则可以很好地插入空值。此外,如果值与 null 不同,则一切正常。

如何插入空值?

4

2 回答 2

0

尝试类似的东西

  e.NewValues["EntityFrameworkEntityPropertyName"] = System.DBNull.Value;
于 2013-06-28T15:48:14.027 回答
0

好的,我找到了解决方案。DetailsView并且FormViewOnItemUpdating事件中比较以前的值和新值,如果这些值发生更改,请更新它。当我将 null 放入e.NewValues时,虽然 myTextBox不受Bound函数限制,e.OldValues["EntityFrameworkEntityPropertyName"]但不存在(意味着 null)并且 inNewValues也为 null,因此不会更新列。

我只是设置e.OldValues["EntityFrameworkEntityPropertyName"]为任何东西(但类型相同),现在插入空值可以正常工作。

protected void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    e.OldValues["EntityFrameworkEntityPropertyName"] = -1
    e.NewValues["EntityFrameworkEntityPropertyName"] = null;
}

我的文本框没有直接限制,因为我需要先处理它的值,然后再将其显示给用户。在插入/更新时,我还需要一些值处理。

于 2013-07-02T11:21:49.247 回答