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?
It seems that the values of invisible
DataControlFields(likeBoundField) 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 theDataKeyNamesproperty of the data-bound control. You can remove then the invisible field from theFieldscollection: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).