【发布时间】:2011-02-24 20:16:53
【问题描述】:
我有两个数据库表,一个用于网站用户,包含字段“UserID”、“Name”和外键“PageID”。另一个带有“PageID”(这里是主键)和“Url”字段。
我希望能够在 gridview 中使用两个表中的数据显示数据,并且我希望通过 aspx 页面中的数据绑定来实现。
不过,我不知道该怎么做,也找不到任何关于这种特殊情况的好例子。这是我目前所拥有的:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="LinqBinding._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Testing LINQ
</h2>
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSourceUsers" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="UserID" HeaderText="UserID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PageID" HeaderText="PageID" />
<asp:TemplateField HeaderText="Pages">
<ItemTemplate
<asp:DropDownList ID="DropDownList1"
DataSourceID="LinqDataSourcePages"
SelectedValue='<%#Bind("PageID") %>'
DataTextField="Url"
DataValueField="PageID"
runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSourcePages" runat="server"
ContextTypeName="LinqBinding.UserDataContext" EntityTypeName=""
TableName="Pages">
</asp:LinqDataSource>
<asp:LinqDataSource ID="LinqDataSourceUsers" runat="server"
ContextTypeName="LinqBinding.UserDataContext" EntityTypeName=""
TableName="Users">
</asp:LinqDataSource>
</asp:Content>
但这仅适用于将用户表放入gridview(这不是问题),并且我将页面数据放入下拉列表中,但问题是:我当然将所有页面数据放入在那里,不仅仅是每一行上每个用户的页面。那么,如何在每一行的下拉列表中设置某种“位置”约束,以仅显示该行中用户的页面? (另外,老实说,我不确定我是否得到了正确的外键关系,因为我不太习惯处理关系)。
编辑:
我认为我错误地建立了关系。我不断收到“页面”不作为用户对象的属性存在的消息。而且我想它不能,因为现在的关系是一种方式。所以我试图建立一个多对多的关系。同样,我的数据库知识有点有限,但我添加了一个所谓的“联结表”,其中包含字段 UserID 和 PageID,与其他表的主键相同。虽然我无法在联结表中创建这两个主键(看起来有些人在我见过的示例中拥有......但因为不可能,我猜他们不应该)。无论如何,我从每个表中创建了一个关系,并从中创建了新的 LINQ 类。
但那我该怎么办?我将联结表设置为 Linq 数据源,因为我猜我必须这样做才能访问这两个表,但这不起作用。然后它抱怨该对象上没有 Name 属性。那么如何访问相关表呢?
(顺便说一句:这是我为寻找解决方案而查看的一页:http://www.iaingalloway.com/2015/06/many-to-many-relationships-in-linq-to-sql.html,但首先我不明白他如何修改“订单”类代码隐藏。我只能进入上下文类(我无法按照他的建议右键单击设计视图中的类并查看代码...),并且在那里似乎没有任何方法可以引用联结类 - 在他的情况下是 Order_Details)...我我错过了什么?)
这是我现在拥有的多对多关系:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ManyToMany._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Many to many LINQ
</h2>
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1" AutoGenerateColumns="false">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="UserID" HeaderText="UserID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="PageID" HeaderText="PageID" />
<asp:TemplateField HeaderText="Pages">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1"
DataSource='<%#Eval("Pages") %>'
SelectedValue='<%#Bind("PageID") %>'
DataTextField="Url"
DataValueField="PageID"
runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="ManyToMany.UserPageDataContext"
EntityTypeName="" TableName="UserPages">
</asp:LinqDataSource>
</asp:Content>
【问题讨论】:
标签: linq data-binding datagrid drop-down-menu