我正在研究可以在數據庫的主表上執行添加,編輯和刪除操作的gridview。在編輯操作,應該在一個下拉控制填充所有外鍵值((如果有的話)。在常規asp.net頁面的Gridview列中使用DynamicDataManager控件加載外鍵值
我碰到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的代碼中提到任何地方。
我的HTML標記爲GridView是:
<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中的一些設置?
你解決這個問題?我看到你在aspnet論壇上發佈了同樣的問題。我在這裏有類似的問題,我認爲這是不可能實現的,而無需在global.asax文件中註冊整個DataContext。 EnableDynamicData方法使用更簡單的MetaTable生成器,根本不考慮相關模型。 – julealgon