是否可以为具有子类的实体对象提供可编辑的详细信息视图?
假设我有两个类,一个从 EntityObject
派生,另一个从第一个类派生:
public class Gizmo : EntityObject { ... }
public class SpecialGizmo : Gizmo { ... }
在 ASP.NET 页面中,用户在列表中选择一个 Gizmo(一个 GridView
),然后 Gizmo 的详细信息将显示在 DetailsView
中。目标是让用户能够查看和编辑详细信息。
以下是相关的 DetailsView
及其关联的 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>
以上失败并出现以下异常:
InvalidOperationException:必须定义 CommandText 或 EntitySetName。
这是可以理解的。但是,提供的两个选项都会破坏某些内容:
如果我添加
EntitySetName="Gizmo"
,则仅显示实际类型Gizmo
的实体。如果选择SpecialGizmo
,DetailsView 将显示为空白。如果我添加
CommandText
(或Select
)属性,则DetailsView
不再支持更新数据。那里有一个可用的“编辑”按钮(使编辑 UI 出现),但是在进行编辑后单击“更新”根本不会执行任何操作。
有没有一个合适的办法来解决这个困境呢?
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 typeGizmo
are ever presented. If aSpecialGizmo
is selected, the DetailsView comes up blank.If I add a
CommandText
(or aSelect
) attribute, then theDetailsView
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用以下 hack 解决了这个问题:
在数据源上指定一个
CommandText
,这使得DetailsView
无法自动更新数据,但更新 UI 是仍然可用。将
DetailsView
的OnItemUpdating
事件设置为如下所示:此解决方案的缺点: 页面上依赖由此更新的数据的其他控件,在用户重新加载手册页之前不会刷新。
I solved this using the following hack:
Do specify a
CommandText
on the data source, which makes theDetailsView
unable to update the data automatically, but the update UI is still available.Set the
DetailsView
’sOnItemUpdating
event to something like this: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.