[英]For each category, display its subcategory
我是 SQL 和 asp.net 的新手,正在嘗試創建一個論壇應用程序。 我創建了兩個表。 我正在使用此查詢選擇表中的數據:
SELECT
forum_categories.CategoryName, forum_subcategories.SubCategoryName
FROM
forum_categories
LEFT JOIN
forum_subcategories ON forum_categories.CategoryId = forum_subcategories.CategoryId
這是查詢返回的內容:
現在我需要的是每個 CategoryName 我需要顯示它的子類別。 我為此使用 ListView:
<asp:ListView ID="ListView1" runat="server" DataSourceID="ForumDataSource">
<ItemTemplate>
<div id='<%#Eval("CategoryName") %>' class="PostCategories">
<h2><asp:Label ID="Label1" runat="server" Text='<%#Eval("CategoryName") %>'></asp:Label></h2>
<table class="ForumPosts">
<thead>
<td>Category</td>
<td>Posts</td>
<td>Last Post</td>
</thead>
<tbody>
<tr>
<td><asp:Label ID="Label2" runat="server" Text='<%#Eval("SubCategoryName") %>'></asp:Label></td>
</tr>
</tbody>
</table>
</div>
</ItemTemplate>
<EmptyDataTemplate>
<p>No Data Found</p>
</EmptyDataTemplate>
</asp:ListView>
<asp:SqlDataSource ID="ForumDataSource" runat="server"
ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT forum_categories.CategoryName, forum_subcategories.SubCategoryName FROM forum_categories LEFT JOIN forum_subcategories ON forum_subcategories.CategoryId = forum_categories.CategoryId">
</asp:SqlDataSource>
現在每個人都可能已經從代碼中了解到這將返回 8 個表。
我想得到的是每個類別顯示它的子類別數據。在這種情況下應該只有 3 個表
我應該如何解決這個問題?
這應該使用 SQL 解決還是使用服務器端語言處理接收到的數據?
我不是 ListViews 的忠實粉絲。 但是你可以試試這個
您需要兩個列表視圖。 外部列表視圖和內部列表視圖。
外層將遍歷類別,因此它應該只給你三個表。 確保您只選擇不同的。
然后內部列表視圖將遍歷每個 CategoryID 並獲取鏈接到 CategoryID 的子類別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.