简体   繁体   English

在同一ListView中显示来自多个DataSources的数据

[英]Show data from several DataSources in the same ListView

I have a ListView and two dropdown controllers in the InsertItemTemplate. 我在InsertItemTemplate中有一个ListView和两个下拉控制器。 Those dropdown lists get their data from another table than the ListViews data. 这些下拉列表从ListViews数据之外的另一个表中获取数据。

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2"
   DataTextField="Genrename" DataValueField="GenreID" 
   SelectedValue='<%# Bind("GenreID") %>'>
</asp:DropDownList>
<br />
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ObjectDataSource3"
   DataTextField="Directorname" DataValueField="RegissorID"
   SelectedValue='<%# Bind("DirectorID") %>'>
</asp:DropDownList>

I am adding values to the main ListView, but the values I insert in the main table is ID values like, GenreID. 我正在将值添加到主ListView,但是我在主表中插入的值是ID值,如GenreID。

My table looks something like this: 我的桌子看起来像这样:

Movie       |    Genre   |    Director
James Bond        3              4
Donald Duck       6              13
The Hangover      7              8

Now, i want to display the Names of the Genre and Director instead of the ID's, but i still need to insert the ID's inte my main table (movies). 现在,我想显示流派和导演的名称而不是ID的名称,但是我仍然需要在我的主表(电影)中插入ID的名称。

This is how my ItemTemplate looks like: 这是我的ItemTemplate的样子:

<td>
   <asp:Label ID="GenreIDLabel" runat="server" Text='<%# Eval("GenreID") %>' />
</td>
<td>
   <asp:Label ID="RegissorIDLabel" runat="server" Text='<%# Eval("RegissorID") %>' />
</td>

Modify the SelectCommand of the datasource control that is being accessed by the ListView. 修改由ListView访问的数据源控件的SelectCommand
Join the three tables

Example: 例:

 SelectCommand="Select m.movie,m.Genre,m.Director,g.GenreName,d.DirectorName From Movies as m Inner Join Genres as g ON m.Genre=g.GenreID Inner Join Directors as d ON m.Director=d.DirectorID"

After this point GenreName and DirectorName are accessible to your gridview. 此后, GenreNameDirectorName可用于您的GenreName Update your ItemTemplate 更新您的ItemTemplate

 <td>
  <asp:Label ID="GenreIDLabel" runat="server" Text='<%# Eval("GenreName") %>' />
</td>
<td>
 <asp:Label ID="RegissorIDLabel" runat="server" Text='<%# Eval("DirectorName") %>' />
</td>

Edit I saw in your comment that you're getting the data from a list. 编辑我在您的评论中看到您正在从列表中获取数据。 I guess it works the same just update your select statement. 我猜只是在更新您的select语句时,它的工作原理相同。 In my example I assumed you're using SqlDataSource control 在我的示例中,我假设您正在使用SqlDataSource控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM