在常规 ASP.NET 页面上的 Gridview 列中使用 DynamicDataManager 控件加载外键值

发布于 2024-12-13 04:22:08 字数 2916 浏览 1 评论 0原文

我正在开发一个 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 中进行一些设置?

I am working on a gridview that can perform add, edit and delete operations on master tables in a database. On Edit operation, It Should populate all foreign key values((if any) in a dropdown control.

I came across dynamicdataManager control to be used for this scenario and found it can be easily used in Asp.net web forms and then there is no need to create a dedicated Dynamic data website.

I am able to bind the data with LinqDataSource but I am unable to load foreign key values in one of the columns. I have set up two table in the database for creating the sample.

Structure of tables are :

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

Parent Table

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]

I have create a LINQDataSource for these two tables and the class relationship is as displayed in the diagram below :

enter image description here

Now, I have dynamic fields in my gridview to show list of LK_CardTypes and one of which refers to the the name of the referenced class "RefTest". When I run my page, I get to see class name instead of a value from the referenced table "RefTest".

I know I am missing something since I haven't mentioned anywhere in the code that Ref_Name is mapped to Ref_Id.

My Html Markup for the gridview is :

<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>

On the Page_Init Event, I have enabled the dynamic data on the gridview and have registered the control with the DynamicDataManager control

  GridView1.EnableDynamicData(typeof(LK_CardType));
          DynamicDataManager1.RegisterControl(GridView1);

Do I need some setting at the field level of Gridview control or need some setting in LinqDataSource ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉黛浅 2024-12-20 04:22:09

您也许能够完成这项工作:

<asp:GridView ID="GridView2" 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:TemplateField>
            <ItemTemplate>
                <%# Eval("RefTest.Ref_Name") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

当然您将无法编辑 RefTest.Ref_Name 值

You might be able to make this work:

<asp:GridView ID="GridView2" 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:TemplateField>
            <ItemTemplate>
                <%# Eval("RefTest.Ref_Name") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Of course you won't be able to edit the RefTest.Ref_Name value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文