使用listview显示来自mysql的同一个表中的多个数据

时间:2015-09-29 05:16:49

标签: asp.net

我使用listview来显示mysql中3个表的数据。  我的表是,

theatredet(theaterid,theatername,locationid)
location(locationid,locationname)
screendet(screenname,theaterid,seatsavailable)

我想在剧院表格中根据剧本来显示来自screendet的数据,我只能从screendet中显示单个数据,该表格上有多个关于剧本的数据。

我的查询是,

string query = "SELECT `theatredetails`.*,`Location`.`LocationName`,
        `screendetails`.`ScreenName`,`screendetails`.`SeatsAvailable` 
        FROM `theatredetails`INNER JOIN `screendetails` 
        ON `screendetails`.`TheatreDetailsId` = `theatredetails`.`TheatreDetailsId` 
        INNER JOIN `location` ON `location`.`LocationId`=`theatredetails`.`LocationId`;";

我的aspx

<asp:TextBox ID="TextBox6" runat="server" Text='<%#Bind("ScreenName") %>'></asp:TextBox>
 <asp:TextBox ID="TextBox7" runat="server" Text='<%#Bind("SeatsAvailable") %>'></asp:TextBox>

1 个答案:

答案 0 :(得分:0)

假设您已经在MySql上测试了您的查询,并且它返回了预期的行数。

您可以使用以下代码填充列表视图(我使用SqlDataSource绑定ListView,您可以使用任何其他方法)

<asp:ListView ID="lstviewScreens" runat="server" DataSourceID="ScreensSqlDataSource" >

<LayoutTemplate>

       <table id="itemPlaceholderContainer" runat="server" >

           <tr id="itemPlaceholder" runat="server">
           </tr>
        </table>

 </LayoutTemplate>

 <ItemTemplate>
     <tr style="background-color: #E0FFFF;">
       <td>
        <strong>Theater Name:</strong><asp:Label ID="lblTheaterName" runat="server" Text='<%# Eval("theatername") %>' />
        <br />
        <strong>Screen Name:</strong><asp:Label ID="lblScreenName" runat="server" Text='<%# Eval("ScreenName") %>' />
         <br />
         <strong>Seats Available</strong><asp:Label ID="lblSeatsAvailable" runat="server" Text='<%# Eval("SeatsAvailable") %>' />
       </td>
     </tr>
   </ItemTemplate>
</asp:ListView>

      <asp:SqlDataSource ID="ScreensSqlDataSource" runat="server" 
        ConnectionString="Your connection String" 
        SelectCommand="SELECT theatredetails.*,Location.LocationName,
    screendetails.ScreenName,screendetails.SeatsAvailable 
    FROM theatredetails INNER JOIN screendetails 
    ON screendetails.TheatreDetailsId = theatredetails.TheatreDetailsId 
    INNER JOIN location ON location.LocationId=theatredetails.LocationId">

    </asp:SqlDataSource>

这将以表格格式输出您的数据,但您可以自定义LayoutTemplate以更改生成的html,即使您可以使用GroupTemplate对数据进行分组。