Search code examples
gridviewformviewlinqdatasource

ASP.NET - FormView bound to LinqDataSource shows data but won't update database (Updating event fires however)


I have a FormView bound to LinqDataSource, that is intended to allow editing of a single product's details, that is selected from a GridView. The LDS has a Where parameter set to point to the GridView control (i.e. a ControlParameter):

<asp:LinqDataSource ID="ldsProduct" runat="server" OnContextCreating="LinqDataSource_ContextCreating"
EnableDelete="True" EnableInsert="True" EnableUpdate="True" TableName="Products"
Where="ProductID=@ProductID" OnSelecting="ldsProduct_Selecting" OnUpdating="ldsProduct_Updating"
OnInserted="ldsProduct_Inserted" OnUpdated="ldsProduct_Updated" OnDeleted="ldsProduct_Deleted">
<WhereParameters>
    <asp:ControlParameter Name="ProductID" ControlID="gvProducts" />
</WhereParameters>

The FormView does show whichever product record I select in the GridView. However when I click Update, no update is run against the database.

This is what I have seen:

  • In the FormView's ItemCommand handler, the Update command happens
  • Next, the LDS's Updating event fires however if I look at e.NewObject there is a Product object attached, but it only has it's ProductID set (to the correct value) - all other properties are not set. No doubt this is why the update never hits the database.

I have tried explicitly setting the FormView to Edit mode, and databinding it in the FormView's ItemCommand handler, but it makes no difference, i.e.:

fvProduct.ChangeMode(FormViewMode.Edit)
fvProduct.DataBind()

I just don't get how the FormView is clearly bound to a Product object when it is showing its data but looses it when the Update command runs.

By the way, I am using a custom constructor for the DataContext, but if I replace OnContextCreated with the usual ContextTypeName the problem still happens.


Solution

  • Figured it out. Viewstate must be turned off by default for FormViews. When EnableViewstate="true" is set, the control behaves "properly" on postback.