Is it possible to have an editable DetailsView for entity objects with subclasses?

自作多情 提交于 2019-12-13 17:07:10

问题


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?


回答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.



来源:https://stackoverflow.com/questions/9080971/is-it-possible-to-have-an-editable-detailsview-for-entity-objects-with-subclasse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!