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:
publ开发者_如何转开发ic 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?
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).
精彩评论