是否有可能有一个可编辑的DetailsView实体与子类的对象? [英] Is it possible to have an editable DetailsView for entity objects with subclasses?
问题描述
假如我有两个类,一个从 EntityObject
衍生另从第一来源:
Suppose I have two classes, one derived from EntityObject
and the other derived from the first:
public class Gizmo : EntityObject { ... }
public class SpecialGizmo : Gizmo { ... }
在ASP.NET页面中,用户选择列表中的一个小物件(一个 GridView控件
)和小物件的细节则$ P $在psented DetailsView控件
。我们的目标是用户能够查看的和编辑的细节。
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.
下面是相关的 DetailsView控件
及其相关联的 EntityDataSource
:
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异常:无论CommandText中或EntitySetName必须定义
InvalidOperationException: Either CommandText or EntitySetName must be defined.
这是可以理解的。然而,这两个选项presented碰坏:
That’s understandable. However, both options presented break something:
-
如果我想补充
EntitySetName =小玩意儿
,那么只有实际类型的实体小玩意儿
是永远presented。如果SpecialGizmo
被选中,DetailsView控件出现空白。
If I add
EntitySetName="Gizmo"
, then only entities of actual typeGizmo
are ever presented. If aSpecialGizmo
is selected, the DetailsView comes up blank.
如果我添加一个的CommandText
(或选择
)的属性,那么 DetailsView控件
不再支持更新数据。一个工作的编辑按钮(使编辑UI显示)是存在的,但后来做点击后更新的编辑根本什么都不做。
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?
推荐答案
我解决了这个使用下面的技巧:
I solved this using the following hack:
-
执行指定
的CommandText
数据源,这使得DetailsView控件
无法更新数据上自动,但更新UI仍然可用。
Do specify a
CommandText
on the data source, which makes theDetailsView
unable to update the data automatically, but the update UI is still available.
将 DetailsView控件
的 OnItemUpdating
事件是这样的:
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;
}
此解决方案的缺点: 依靠这是正是如此更新的数据页面上的其他控件,不刷新直到被用户手动重新加载页面。
这篇关于是否有可能有一个可编辑的DetailsView实体与子类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!