Asp.net 是否可以为具有子类的实体对象创建可编辑的详细信息视图?
假设我有两个类,一个派生自Asp.net 是否可以为具有子类的实体对象创建可编辑的详细信息视图?,asp.net,detailsview,Asp.net,Detailsview,假设我有两个类,一个派生自EntityObject,另一个派生自第一个: public class Gizmo : EntityObject { ... } public class SpecialGizmo : Gizmo { ... } 在ASP.NET页面中,用户在列表(一个GridView)中选择一个Gizmo,然后该Gizmo的详细信息显示在DetailsView中。目标是让用户能够查看和编辑详细信息 以下是相关的DetailsView及其关联的EntityDataSource: &
EntityObject
,另一个派生自第一个:
public class Gizmo : EntityObject { ... }
public class SpecialGizmo : Gizmo { ... }
在ASP.NET页面中,用户在列表(一个GridView
)中选择一个Gizmo,然后该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>
上述操作失败,但出现以下例外情况:
无效操作异常:必须定义CommandText或EntitySetName
这是可以理解的。然而,这两个选项都会破坏某些东西:
- 如果添加
,则仅会显示实际类型的EntitySetName=“Gizmo”
实体。如果选择了Gizmo
,则详细信息视图将显示为空白SpecialGizmo
- 如果我添加了
(或CommandText
)属性,则Select
不再支持更新数据。一个工作的“编辑”按钮(使编辑界面出现)在那里,但在编辑后点击“更新”根本不起作用DetailsView
有没有合适的方法来解决这个难题?我用以下方法解决了这个问题:
- 在数据源上指定一个
,这会使CommandText
无法自动更新数据,但更新UI仍然可用DetailsView
- 将
的DetailsView
事件设置为如下内容:onimupdating
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; }
CommandText
部分:“分配CommandText属性时,将禁用更新、插入和删除功能”。六羟甲基三聚氰胺六甲醚。。。