使用嵌套重复器显示部分分层数据

发布于 2024-09-27 12:08:48 字数 2908 浏览 4 评论 0原文

我已经很接近了,但到目前为止。 我有两张表,一张用于发布详细信息,另一个用于类别邻接列表。

我正在尝试使用嵌套中继器显示完整的数据集(除非有更好的方法) 我正在构建我的数据集,如下所示:

string strSql = "SELECT category_id, parent_id, cat_name_en " +
                "FROM categories;" +
                "SELECT pub_id, title_en, category_id " +
                "FROM publications;";

SqlConnection conn = new SqlConnection(connString.ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

DataSet _ds = new DataSet();   
da.Fill(_ds);
_ds.DataSetName = "categories";
_ds.Tables[0].TableName = "category";
_ds.Tables[1].TableName = "publications";

_ds.Relations.Add("ParentChild", 
                  _ds.Tables["category"].Columns["category_id"],
                  _ds.Tables["category"].Columns["parent_id"], false);
_ds.Relations[0].Nested = true;

_ds.Relations.Add("CategoryMembers", 
                  _ds.Tables["category"].Columns["category_id"],
                  _ds.Tables["publications"].Columns["category_id"], false);
_ds.Relations[1].Nested = true;


categoryRepeater.DataSource = _ds.Tables["category"];
categoryRepeater.DataBind();

我正在显示这样的数据:

<asp:Repeater runat="server" ID="categoryRepeater" EnableViewState="false">
  <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "category_id") %>
    <%# DataBinder.Eval(Container.DataItem, "cat_name_en") %><br />

    <asp:Repeater runat="server" EnableViewState="false"
      DataSource='<%# GetChildRelation(Container.DataItem, "CategoryMembers") %>'>  
      <ItemTemplate>
        <p><%# DataBinder.Eval(Container.DataItem, "pub_id") %>&nbsp;
           <%# DataBinder.Eval(Container.DataItem, "title_en") %></p>
      </ItemTemplate>
    </asp:Repeater>

    <asp:Repeater runat="server" EnableViewState="false" 
      DataSource='<%# GetChildRelation(Container.DataItem, "ParentChild") %>'>
      <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "category_id") %>
        <%# DataBinder.Eval(Container.DataItem, "cat_name_en") %><br />  
        <asp:Repeater runat="server" EnableViewState="false"
          DataSource='<%# GetChildRelation(Container.DataItem, "CategoryMembers") %>'>
          <ItemTemplate>
            <p><%# DataBinder.Eval(Container.DataItem, "pub_id") %>
               <%# DataBinder.Eval(Container.DataItem, "title_en") %></p> 
          </ItemTemplate>      
        </asp:Repeater>
      </ItemTemplate>    
    </asp:Repeater>
  </ItemTemplate>
</asp:Repeater>

我像这样嵌套了重复器,因为某些类别没有子类别,但仍然有出版物。

因此,我已经建立了关系来获取任何儿童类别,然后获取属于该类别的任何出版物。 我得到了他们的孩子的类别列表以及属于该类别的任何出版物。

问题是我不知道如何防止某些儿童类别被视为父母。有些类别没有父级,但仍然有出版物。这些类别将在其父类别下正确显示,然后再次作为其自己的父类别

如何配置表或关系,以便子类别不会被视为父类别?

I'm SO close, but so far.
I've got two tables, one for publication details, the other for the categories adjacency list.

I'm trying to display the full dataset with nested repeaters (unless there's a better way)
and I'm building my DataSet as shown:

string strSql = "SELECT category_id, parent_id, cat_name_en " +
                "FROM categories;" +
                "SELECT pub_id, title_en, category_id " +
                "FROM publications;";

SqlConnection conn = new SqlConnection(connString.ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);

DataSet _ds = new DataSet();   
da.Fill(_ds);
_ds.DataSetName = "categories";
_ds.Tables[0].TableName = "category";
_ds.Tables[1].TableName = "publications";

_ds.Relations.Add("ParentChild", 
                  _ds.Tables["category"].Columns["category_id"],
                  _ds.Tables["category"].Columns["parent_id"], false);
_ds.Relations[0].Nested = true;

_ds.Relations.Add("CategoryMembers", 
                  _ds.Tables["category"].Columns["category_id"],
                  _ds.Tables["publications"].Columns["category_id"], false);
_ds.Relations[1].Nested = true;


categoryRepeater.DataSource = _ds.Tables["category"];
categoryRepeater.DataBind();

I'm displaying the data like this:

<asp:Repeater runat="server" ID="categoryRepeater" EnableViewState="false">
  <ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem, "category_id") %>
    <%# DataBinder.Eval(Container.DataItem, "cat_name_en") %><br />

    <asp:Repeater runat="server" EnableViewState="false"
      DataSource='<%# GetChildRelation(Container.DataItem, "CategoryMembers") %>'>  
      <ItemTemplate>
        <p><%# DataBinder.Eval(Container.DataItem, "pub_id") %> 
           <%# DataBinder.Eval(Container.DataItem, "title_en") %></p>
      </ItemTemplate>
    </asp:Repeater>

    <asp:Repeater runat="server" EnableViewState="false" 
      DataSource='<%# GetChildRelation(Container.DataItem, "ParentChild") %>'>
      <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "category_id") %>
        <%# DataBinder.Eval(Container.DataItem, "cat_name_en") %><br />  
        <asp:Repeater runat="server" EnableViewState="false"
          DataSource='<%# GetChildRelation(Container.DataItem, "CategoryMembers") %>'>
          <ItemTemplate>
            <p><%# DataBinder.Eval(Container.DataItem, "pub_id") %>
               <%# DataBinder.Eval(Container.DataItem, "title_en") %></p> 
          </ItemTemplate>      
        </asp:Repeater>
      </ItemTemplate>    
    </asp:Repeater>
  </ItemTemplate>
</asp:Repeater>

I've nested the repeaters like that because some categories don't have children categories, but still have publications.

So I've got the Relations in place to get any Children categories, and then get any publications belonging to that category.
I get the listing of categories with their children and any publications that belong to the category.

The problem is I can't figure out how to prevent some of the Children categories being treated as Parents. There are some categories that do not have parents, but still have publications. These categories will be displayed correctly under their parent and then AGAIN as their own Parent category

How do I configure the tables or relations so children categories aren't treated like parents?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2024-10-04 12:08:48

首先,您需要创建一个 DataView,其 RowFilter 为“parent_id is null”或parent_id = '',具体取决于数据是什么,

然后绑定该数据视图而不是表。

例如,

DataView dv = new DataView(_ds.Tables["category"]);
dv.RowFilter = "parent_id is null";

categoryRepeater.DataSource = dv;

您不必触摸 GetChildRelation 函数,因为它仍然会从 Container.DataItem 获取 DataRowView

First you'll need to create a DataView with the RowFilter of either "parent_id is null" or parent_id = '' depending on what the data is

Next bind that dataview instead of the table.

e.g.

DataView dv = new DataView(_ds.Tables["category"]);
dv.RowFilter = "parent_id is null";

categoryRepeater.DataSource = dv;

You shouldn't have to touch your GetChildRelation function as it will still get a DataRowView from Container.DataItem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文