我正在开发一个可以对数据库中的主表执行添加、编辑和删除操作的 gridview。在编辑操作中,它应该填充下拉控件中的所有外键值((如果有)。
我遇到了用于此场景的 dynamicdataManager 控件,发现它可以在 Asp.net Web 表单中轻松使用,然后无需创建专用的动态数据网站。
我能够将数据与 LinqDataSource 绑定,但我无法在其中一列中加载外键值。我在数据库中设置了两个表来创建示例。
表的结构是:
CREATE TABLE [dbo].[LK_CardType](
[CardTypeID] [int] IDENTITY(1,1) NOT NULL,
[CardTypeName] [nvarchar](50) NOT NULL,
[AdditionalNotes] [nvarchar](50) NOT NULL,
[Ref_ID] [int] NULL, --this is referring to another table called RefTes(ReferenceTest)
CONSTRAINT [PK_LK_CardType] PRIMARY KEY CLUSTERED ( [CardTypeID] ASC ) ) ON [PRIMARY]
GO
ALTER TABLE [dbo].[LK_CardType] WITH CHECK ADD CONSTRAINT [fk_cardtype_RefTest] FOREIGN KEY([Ref_ID])
REFERENCES [dbo].[RefTest] ([Ref_Id])
GO
父表
CREATE TABLE [dbo].[RefTest](
[Ref_Id] [int] IDENTITY(1,1) NOT NULL,
[Ref_Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_RefTest] PRIMARY KEY CLUSTERED
(
[Ref_Id] ASC
)
) ON [PRIMARY]
我为这两个表创建了一个 LINQDataSource,类关系如下图所示:
现在,我的 gridview 中有动态字段来显示 LK_CardTypes 列表,其中一个是引用类“RefTest”的名称。当我运行我的页面时,我会看到类名而不是引用表“RefTest”中的值。
我知道我遗漏了一些东西,因为我没有在代码中提到 Ref_Name 映射到 Ref_Id 的任何地方。
我的 gridview 的 Html 标记是:
<table>
<tr>
<td colspan="3">
<asp:DynamicDataManager ID="DynamicDataManager1" AutoLoadForeignKeys="true" runat="server">
<%-- <DataControls><asp:DataControlReference ControlID="GridView1" /></DataControls> --%>
</asp:DynamicDataManager>
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CardTypeID" DataSourceID="LinqDataSource" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:DynamicField DataField="CardTypeID" />
<asp:DynamicField DataField="CardTypeName" />
<asp:DynamicField DataField="AdditionalNotes" />
<asp:DynamicField DataField="RefTestValue" />
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource" runat="server"
ContextTypeName="JqueryGrid_MasterData.MasterTableDataContext"
EntityTypeName="" TableName="LK_CardTypes" EnableDelete="True"
EnableInsert="True" EnableUpdate="True">
</asp:LinqDataSource>
在 Page_Init 事件中,我在 gridview 上启用了动态数据,并已将控件注册到 DynamicDataManager 控件
GridView1.EnableDynamicData(typeof(LK_CardType));
DynamicDataManager1.RegisterControl(GridView1);
我需要在 Gridview 控件的字段级别进行一些设置还是需要在 LinqDataSource 中进行一些设置?