简体   繁体   English

对于每个类别,显示其子类别

[英]For each category, display its subcategory

I am kind of new to SQL and asp.net and am trying to create a forum application.我是 SQL 和 asp.net 的新手,正在尝试创建一个论坛应用程序。 I have created two tables.我创建了两个表。 And I am selecting the data in the tables using this query:我正在使用此查询选择表中的数据:

SELECT 
    forum_categories.CategoryName, forum_subcategories.SubCategoryName  
FROM     
    forum_categories  
LEFT JOIN 
    forum_subcategories ON forum_categories.CategoryId = forum_subcategories.CategoryId

在此处输入图片说明

This is what the query returns:这是查询返回的内容:

在此处输入图片说明

Now what I need is for each CategoryName I need to display it's subcategories.现在我需要的是每个 CategoryName 我需要显示它的子类别。 I am using a ListView for this:我为此使用 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>

Now as everyone might have already understood from the code this returns 8 tables.现在每个人都可能已经从代码中了解到这将返回 8 个表。

What I would like to get is for each category to display it's subcategory data.In this case there should be only 3 tables我想得到的是每个类别显示它的子类别数据。在这种情况下应该只有 3 个表

How should I approach this problem?我应该如何解决这个问题?

Should this be solved using SQL or manipulating the received data with a server side language?这应该使用 SQL 解决还是使用服务器端语言处理接收到的数据?

I'm not a big fan of ListViews.我不是 ListViews 的忠实粉丝。 But you can try this out,但是你可以试试这个

You need two list views.您需要两个列表视图。 Outer listview and inner listview.外部列表视图和内部列表视图。

Outer one will loop through the categories hence it should give you only three tables.外层将遍历类别,因此它应该只给你三个表。 Make sure you select only the distinct ones.确保您只选择不同的。

Then inner listview will loop through each CategoryID and fetch the sub categories linked to CategoryID.然后内部列表视图将遍历每个 CategoryID 并获取链接到 CategoryID 的子类别。

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

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