2

Suppose I have two classes, one derived from EntityObject and the other derived from the first:

public class Gizmo : EntityObject { ... }
public class SpecialGizmo : Gizmo { ... }

In the ASP.NET page, the user selects a Gizmo in a list (a GridView) and that Gizmo’s details are then presented in a DetailsView. The goal is for the user to be able to view and edit the details.

Here is the relevant DetailsView and its associated EntityDataSource:

<asp:DetailsView ID="GizmosDetailsView" DataSourceID="dsGizmoDetails"
    AutoGenerateEditButton="True" AutoGenerateInsertButton="True"
    AutoGenerateRows="False" DataKeyNames="GizmoId" runat="server">
    <Fields>
        <asp:BoundField DataField="GizmoId" HeaderText="GizmoId" ReadOnly="True" SortExpression="GizmoId" />
        <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        <!-- ... etc. --->
    </Fields>
</asp:DetailsView>

<asp:EntityDataSource ID="dsGizmoDetails" runat="server" ConnectionString="[...]"
    DefaultContainerName="[...]" EnableFlattening="False" EnableUpdate="True"
    Where="it.[GizmoId] = @GizmoId">
    <WhereParameters>
        <asp:ControlParameter ControlID="gvwGizmos" Name="GizmoId" PropertyName="SelectedValue" Type="Int64" />
    </WhereParameters>
</asp:EntityDataSource>

The above fails with the following exception:

InvalidOperationException: Either CommandText or EntitySetName must be defined.

That’s understandable. However, both options presented break something:

  • If I add EntitySetName="Gizmo", then only entities of actual type Gizmo are ever presented. If a SpecialGizmo is selected, the DetailsView comes up blank.

  • If I add a CommandText (or a Select) attribute, then the DetailsView no longer supports updating the data. A working “edit” button (that makes edit UI appear) is there, but then clicking “Update” after making edits simply does nothing.

Is there a proper solution to this dilemma?

Timwi
  • 65,159
  • 33
  • 165
  • 230
  • Well [this MSDN article](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.entitydatasource.commandtext.aspx) explains the `CommandText` part: "When the CommandText property is assigned, update, insert, and delete functionalities are disabled". Hmmm... – Josh Darnell Jan 31 '12 at 14:30

1 Answers1

1

I solved this using the following hack:

  • Do specify a CommandText on the data source, which makes the DetailsView unable to update the data automatically, but the update UI is still available.

  • Set the DetailsView’s OnItemUpdating event to something like this:

    protected void GizmoDetailsView_Updating(object sender,
            DetailsViewUpdateEventArgs e)
    {
        db.ExecuteStoreCommand(/* use e.Keys["GizmoId"] and e.NewValues */);
        db.SaveChanges();
    
        // manually set the DetailsView back to read-only mode
        GizmoDetailsView.ChangeMode(DetailsViewMode.ReadOnly);
    
        // need to cancel the event, as otherwise we get the following exception:
        // InvalidOperationException: Update is disabled for this control.
        e.Cancel = true;
    }
    

Downside of this solution: other controls on the page that rely on the data which is thusly updated, do not refresh until a manual page reload by the user.

Community
  • 1
  • 1
Timwi
  • 65,159
  • 33
  • 165
  • 230