我真的很新的ASP.NET和我已經看了無數的教程,試圖弄清楚這一點,但似乎無法讓我的頭周圍。我可以從tbProject成功輸出記錄使用ListView和實體框架,但代替refDepartmentID,我想從refDepartment表顯示refDepartmentValue。這是一個快速表結構。使用ListView控件來顯示數據,但加入一個參考表
tbProject
-ProjectID
-refDepartmentID
-ProjectName
refDepartment
-refDepartmentID
-refDeparmentValue
我可以在SQL中編寫這個JOIN沒有問題,但我不知道我在哪裏放它。我已經玩了一下LINQ,看起來這就是我要如何實現這一點。也許在後面的代碼中編寫LINQ,然後將它綁定到ListView控件,也許?
這裏的ListView控件:
<asp:ListView runat="server"
DataSourceID="EntityDataSource1">
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="ProjectIDLabel" runat="server" Text='<%# Eval("ProjectID") %>' />
</td>
<td>
<asp:Label ID="refDepartmentIDLabel" runat="server"
Text='<%# Eval("refDepartmentID") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">
ProjectID</th>
<th runat="server">
refDepartmentID</th>
</tr>
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
這裏是我如何使用EF來填充一個下拉列表,所以你至少可以指事物的命名方式。
// populates Departments dropdownlist
using (dbOrganizationEntities1 myEntities = new dbOrganizationEntities1())
{
var allDepartments = from refDepartments in myEntities.refDepartments
select refDepartments;
ddlDepartments.DataSource = allDepartments;
ddlDepartments.DataValueField = "refDepartmentID";
ddlDepartments.DataTextField = "refDepartmentValue";
ddlDepartments.DataBind();
}
任何幫助,將不勝感激。謝謝!
謝謝真是棒極了! – user1431633