【问题标题】:How to access ForeignKey values in GridView using LinqDataSource?如何使用 LinqDataSource 访问 GridView 中的 ForeignKey 值?
【发布时间】:2011-02-08 19:53:02
【问题描述】:

问题如下:

我在数据库中有三个表:

ARTICLE 包含 ID_Article、Title 等字段以及 FK ID_Author

具有 ID_Category、名称字段的 CATEGORY

CATEGORYTOARTICLE 字段 ID_Article、ID_Category

现在我有一个页面显示给定类别的所有文章。 里面有一个GridView。

        <asp:GridView ID="GridViewCategories" runat="server" 
            AutoGenerateColumns="False" DataSourceID="LinqSourceGridCategories" >
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>Title</HeaderTemplate>
                    <ItemTemplate>
                        ???
                    </ItemTemplate>
                </asp:TemplateField>                        
            </Columns>
        </asp:GridView>

加上一个 LinqDataSource

        <asp:LinqDataSource ID="LinqSourceGridCategories" runat="server" 
            ContextTypeName="konserwatyzm.db.DataClassesDataContext" EntityTypeName="" 
            TableName="CategoryToArticles" Where="ID_Category == @ID_Category">
            <WhereParameters>
                <asp:QueryStringParameter Name="ID_Category" QueryStringField="id" 
                    Type="Int32" />
            </WhereParameters>
        </asp:LinqDataSource>

查看的Category的ID是通过QueryString传递的。

如何访问 Article 表的字段以便我可以在 GridView 中为它们创建列? 我尝试使用 Eval("Article.Title") 但它一直说“DataBinding: 'konserwatyzm .db.CategoryToArticle' 不包含名为 'Article' 的属性。”

谢谢!

【问题讨论】:

    标签: c# asp.net database linq


    【解决方案1】:

    确保在 dbml 或映射中正确定义了密钥。然后尝试使用 linqdatasource 配置向导通过选择您想要行的所有表来构造查询。看起来您只在需要时选择了 CategoryToArticles 表+其他两个表。您将能够以这种方式引用其他表中的列。

    【讨论】:

    • 如何在 linqdatasource 配置向导中选择多个表?有可能吗?
    猜你喜欢
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2011-07-12
    • 1970-01-01
    • 2010-09-06
    • 2021-01-23
    相关资源
    最近更新 更多