[英]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.