为什么 ObjectDataSource 中的 UpdateMethod 只接收来自 DetailsView 中可见控件的属性值?

发布于 2024-08-27 02:02:34 字数 1490 浏览 6 评论 0原文

我编写了一个类,其中包含 ObjectDataSource 的选择方法和更新方法。 UpdateMethod 接收被调用类的实例。我的问题是,只有在 DetailsView 中绑定的属性被设置,其他属性都有默认值。

这是我的代码:

类声明

public class Foo
{
  public string Prop1 {get;set:}
  public int Prop2 {get;set;}
}

更新方法

[DataObjectMethod(DataObjectMethodType.Update)]
public static void UpdateQuicklink(Foo item)
{
//  item.Prop1 // contains correct value
// item.Prop2 // is 0
}

标记

<asp:DetailsView ID="DetailsView1" runat="server" 
    DataSourceID="ods" EnableModelValidation="True" AutoGenerateInsertButton="True"
    AutoGenerateRows="False" AutoGenerateEditButton="True">
    <Fields>
        <asp:BoundField DataField="Prop1"/>
        <asp:BoundField DataField="Prop2" Visible="false"/>
    </Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="ods" runat="server"
    TypeName="NamespaceToClassContaingUpdateMethod"
    OldValuesParameterFormatString="original_{0}" 
    DataObjectTypeName="NamespaceToFoo" 
    UpdateMethod="UpdateQuicklink">
</asp:ObjectDataSource>

我无法将我需要的每个字段公开给标记。
一个可能的解决方案是重写我的 UpdateMethod 以接受所有必要的参数,如下所示:

[DataObjectMethod(DataObjectMethodType.Update)]
public static void UpdateQuicklink(string Prop1, int Prop2)
{

}

但是这个解决方案很糟糕,由于我,如果我尝试更改底层数据结构,它不够灵活。我知道在这种情况下我必须编辑我的代码,但我只能将我的自定义包装类作为参数。这可能吗?

I've written a class that contains Select- and Update-Methods for an ObjectDataSource. The UpdateMethod receives an instance of a called class. My problem is, that only properties that are Bound in the DetailsView are set, the others have their default value.

Here's my code:

Class declaration:

public class Foo
{
  public string Prop1 {get;set:}
  public int Prop2 {get;set;}
}

Updatemethod:

[DataObjectMethod(DataObjectMethodType.Update)]
public static void UpdateQuicklink(Foo item)
{
//  item.Prop1 // contains correct value
// item.Prop2 // is 0
}

Markup:

<asp:DetailsView ID="DetailsView1" runat="server" 
    DataSourceID="ods" EnableModelValidation="True" AutoGenerateInsertButton="True"
    AutoGenerateRows="False" AutoGenerateEditButton="True">
    <Fields>
        <asp:BoundField DataField="Prop1"/>
        <asp:BoundField DataField="Prop2" Visible="false"/>
    </Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="ods" runat="server"
    TypeName="NamespaceToClassContaingUpdateMethod"
    OldValuesParameterFormatString="original_{0}" 
    DataObjectTypeName="NamespaceToFoo" 
    UpdateMethod="UpdateQuicklink">
</asp:ObjectDataSource>

I can't expose every field I need to the markup.
A possible solution would be to rewrite my UpdateMethod to accept all necessary parameters, like that:

[DataObjectMethod(DataObjectMethodType.Update)]
public static void UpdateQuicklink(string Prop1, int Prop2)
{

}

But this solution is crap, due to I it is not flexible enough, if I attempt changes to the underlying datastructure. I know that in that case I'd have to edit my code nevertheless, but I'd to only have my custom wrapper class as parameter. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2024-09-03 02:02:34

似乎不可见的 DataControlFields 的值(如 BoundField)不包含在 ViewState 中,因此在往返期间不会保留。 这里是有关该问题的讨论。微软的建议这里是将不可见字段的字段名称添加到数据绑定控件的 DataKeyNames 属性中。然后,您可以从 Fields 集合中删除不可见字段:

<asp:DetailsView ID="DetailsView1" runat="server" 
    DataSourceID="ods" EnableModelValidation="True" AutoGenerateInsertButton="True"
    AutoGenerateRows="False" AutoGenerateEditButton="True"
    DataKeyNames="Prop2">
    <Fields>
        <asp:BoundField DataField="Prop1"/>
    </Fields>
</asp:DetailsView>

这对于模板中的控件来说不是必需的 - 就像使用 Text='<% 绑定的 FormView 的 EditItemTemplate 中的文本框一样# Bind("Prop2") %>'.这里,即使对于不可见的 TextBox,ViewState 在往返过程中也会被保留(当然,除非您禁用 ViewState)。

It seems that the values of invisible DataControlFields (like BoundField) are not included in the ViewState and therefore not preserved during a roundtrip. Here is a discussion about the issue. Microsofts recommendation here is to add the field name for invisible fields to the DataKeyNames property of the data-bound control. You can remove then the invisible field from the Fields collection:

<asp:DetailsView ID="DetailsView1" runat="server" 
    DataSourceID="ods" EnableModelValidation="True" AutoGenerateInsertButton="True"
    AutoGenerateRows="False" AutoGenerateEditButton="True"
    DataKeyNames="Prop2">
    <Fields>
        <asp:BoundField DataField="Prop1"/>
    </Fields>
</asp:DetailsView>

That's not necessary for Controls in a Template - like a TextBox in an EditItemTemplate of a FormView which is bound using Text='<%# Bind("Prop2") %>'. Here ViewState is preserved during roundtrips even for an invisible TextBox (unless you disable ViewState of course).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文