为什么在ObjectDataSource控件的UpdateMethod只为在可见的DetailsView控件的属性接收值? [英] Why does the UpdateMethod in ObjectDataSource only receive values for properties from visible controls in DetailsView?

查看:266
本文介绍了为什么在ObjectDataSource控件的UpdateMethod只为在可见的DetailsView控件的属性接收值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了包含选择 - 和一个ObjectDataSource更新的方法的类。该UpdateMethod收到一个叫做类的一个实例。我的问题是,只有那些在DetailsView绑定属性都设置了,该有的都有了自己的默认值。

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.

下面是我的code:

类声明

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
}

标记

<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接受所有必要的参数,这样的:

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)
{

}

但这种方法是胡扯,因为我把它不够灵活,如果我试图改变底层数据结构。我知道,在这种情况下,我不得不不过编辑我的code,但我只能有我的自定义包装类作为参数。这可能吗?

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?

推荐答案

似乎看不见值 DataControlFields (如的BoundField )不包括在ViewState和往返过程中因此不preserved。 这里是一个关于这个问题的讨论。微软推荐<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.visible.aspx\"相对=nofollow>这里是为无形领域的数据绑定控件的的DataKeyNames 属性添加字段名称。您可以从字段集合然后取出隐形领域:

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>

这是没有必要的控制在一个模板 - 就像这是使用文本约束的FormView控件的EditItemTemplate的一个TextBox ='&LT;%#绑定(Prop2)%&GT; 。这里的ViewState甚至对于一种无形的文本框的往返过程中pserved(除非您禁用当然ViewState中)$ P $。

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).

这篇关于为什么在ObjectDataSource控件的UpdateMethod只为在可见的DetailsView控件的属性接收值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆