ASP.NET 中继器和输出分层数据

发布于 2024-11-02 15:41:03 字数 3419 浏览 0 评论 0原文

我目前正在使用这个 http://blogs.sitepoint.com/hierarchical-data-database -2/ 存储我的数据的方法。

我当前的数据库行如下所示:

名称|节点左|节点右 | 级别

节点 1 | 6 | 1

内容A | 2 | 5 | 2

内容B | 3 | 4 | 2

关于 | 7 | 8 | 1

现在我使用 ASP.NET Repeater 使用列表和 jsTree 输出数据:

<asp:Repeater ID="rptContentSectionGetAll" runat="server">
        <HeaderTemplate>
            <div id="contentSectionTree">
                <ul>
                    <li id="contentSectionTreeRoot" rel="root"><a href="#">Root</a>
                        <ul>
        </HeaderTemplate>
        <ItemTemplate>
                            <li id='<%# Eval("ID") %>'>
                                <a href="#"><%# Eval("name") %></a>
                            </li>

        </ItemTemplate>
        <FooterTemplate>
                        </ul>
                    </li>
                </ul>
            </div>
        </FooterTemplate>
 </asp:Repeater>

我需要将 ItemTemplate 中的内容替换为所有

  • 和嵌套在代码后面的
  • 这是我当前的代码:

    private DataTable RepeaterDataSource()
        {
            ContentSectionBAL cBAL = new ContentSectionBAL(); 
            DataTable dTable = new DataTable();
            try
            {
                dTable = cBAL.Load();
    
                int counter = 1;
                var htmlList = "";
                for (int i = 0; i < dTable.Rows.Count; i++)
                {
                    htmlList = "<li rel='folder' id='" + dTable.Rows[i]["ID"] + "'>";
                    htmlList += "<a href='#'>" + dTable.Rows[i]["name"] + "</a>";
    
                    if (Convert.ToInt32(dTable.Rows[i]["nodeLevel"]) > counter)
                    {
                        htmlList = "<ul>";
                        for (int j = 0; j < dTable.Rows.Count; j++)
                        {
                            if (Convert.ToInt32(dTable.Rows[i]["nodeLevel"]) > counter + 1)
                            {
                                htmlList += "<li rel='file' id='" + dTable.Rows[j]["ID"] + "'>";
                                htmlList += "<a href='#'>" + dTable.Rows[j]["name"] + "</a>";
                                htmlList += "</li>";
                            }
                        }
                        htmlList += "</ul>";
                    }
    
                    htmlList += "</li>";
                    counter = counter + 1;
                }
            }
            catch (Exception ee)
            {
                Session["message"] = ee.Message.ToString();
                Session["messageType"] = "error";
            }
    
            return dTable;
        }
    

    在这个函数之后不久,我通过以下方式绑定它:

    private void BindRepeater()
        {
            rptContentSectionGetAll.DataSource = RepeaterDataSource();
            rptContentSectionGetAll.DataBind();
        }
    

    所有没有子输出的常规节点:

  • name
  • 所有具有子节点的节点都需要以某种循环输出其所有子节点:
  • 我有问题: 1. 循环此类数据以获得正确输出的正确方法是什么? 2.如何在中继器中成功输出? [答案]

    我知道我的方法行不通,但一想到这一切我的大脑就开始煎熬。任何帮助将不胜感激。

    如果您需要任何其他信息,请告诉我,但这应该是这样。

    来吧,伙计们,我需要条件或数学方面的帮助,才能以正确的顺序获取我想要的东西。

    I am currently using this http://blogs.sitepoint.com/hierarchical-data-database-2/ method of storing my data.

    My current database rows look like this:

    Name | nodeLeft | nodeRight | nodeLevel

    Home | 1 | 6 | 1

    ContentA | 2 | 5 | 2

    ContentB | 3 | 4 | 2

    About | 7 | 8 | 1

    Now I am using a ASP.NET Repeater to output the data using lists and jsTree:

    <asp:Repeater ID="rptContentSectionGetAll" runat="server">
            <HeaderTemplate>
                <div id="contentSectionTree">
                    <ul>
                        <li id="contentSectionTreeRoot" rel="root"><a href="#">Root</a>
                            <ul>
            </HeaderTemplate>
            <ItemTemplate>
                                <li id='<%# Eval("ID") %>'>
                                    <a href="#"><%# Eval("name") %></a>
                                </li>
    
            </ItemTemplate>
            <FooterTemplate>
                            </ul>
                        </li>
                    </ul>
                </div>
            </FooterTemplate>
     </asp:Repeater>
    

    I need to replace what's in the ItemTemplate with all <li>'s and nested <ul>'s from the code behind.

    This is my current code behind:

    private DataTable RepeaterDataSource()
        {
            ContentSectionBAL cBAL = new ContentSectionBAL(); 
            DataTable dTable = new DataTable();
            try
            {
                dTable = cBAL.Load();
    
                int counter = 1;
                var htmlList = "";
                for (int i = 0; i < dTable.Rows.Count; i++)
                {
                    htmlList = "<li rel='folder' id='" + dTable.Rows[i]["ID"] + "'>";
                    htmlList += "<a href='#'>" + dTable.Rows[i]["name"] + "</a>";
    
                    if (Convert.ToInt32(dTable.Rows[i]["nodeLevel"]) > counter)
                    {
                        htmlList = "<ul>";
                        for (int j = 0; j < dTable.Rows.Count; j++)
                        {
                            if (Convert.ToInt32(dTable.Rows[i]["nodeLevel"]) > counter + 1)
                            {
                                htmlList += "<li rel='file' id='" + dTable.Rows[j]["ID"] + "'>";
                                htmlList += "<a href='#'>" + dTable.Rows[j]["name"] + "</a>";
                                htmlList += "</li>";
                            }
                        }
                        htmlList += "</ul>";
                    }
    
                    htmlList += "</li>";
                    counter = counter + 1;
                }
            }
            catch (Exception ee)
            {
                Session["message"] = ee.Message.ToString();
                Session["messageType"] = "error";
            }
    
            return dTable;
        }
    

    Shortly after this function I bind it by doing:

    private void BindRepeater()
        {
            rptContentSectionGetAll.DataSource = RepeaterDataSource();
            rptContentSectionGetAll.DataBind();
        }
    

    All regular nodes with NO children outputs: <li>name</li>
    All nodes WITH children needs to output all of it's children in some sort of loop:
    <li><ul><li></li></ul><li>

    Questions I have:
    1. What is the proper way to loop through this type of data for proper output?
    2. How do I successfully output it in a repeater? [ANSWER]

    I know what I have won't work but my brain started frying thinking about it all. Any help would be greatly appreciated.

    If you require anything else let me know but this should be it.

    Come on guys I need help with the conditionals or math to grab what i want in the correct order.

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

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

    发布评论

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

    评论(3

    冷︶言冷语的世界 2024-11-09 15:41:03

    使用 OnItemDataBound 事件来呈现您的代码。将 Item 模板的内容替换为

    <ItemTemplate>       
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </ItemTemplate> 
    

    Then 在 ItemDataBound 事件处理程序中构建所需的字符串结构并将其分配给文字 Text 属性。

    ((Literal)e.Item.FindControl("Literal1")).Text = MyNewStructureString;
    

    Us the OnItemDataBound event to render your code. Replace the contents of your Item template with

    <ItemTemplate>       
        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
    </ItemTemplate> 
    

    Then in your ItemDataBound Event handler build the structure you want as a string and assign it to the literal Text property.

    ((Literal)e.Item.FindControl("Literal1")).Text = MyNewStructureString;
    
    倾城泪 2024-11-09 15:41:03

    您可以使用嵌套的转发器并避免编写所有代码来输出内部 ul。只要您需要的列表是外部转发器的数据项对象的属性,您就可以在转发器内部以声明方式设置转发器的数据源。

    如果这是不可能的,因为对象不是以这种方式构造的,您仍然可以使用嵌套转发器方法,只是您必须在 ItemDataBound 事件中绑定每个内部转发器。

    You could use a nested repeater and avoid writing all the code to output the inner ul. You can declaratively set the datasource of a repeater inside a repeater as long as the list you need is a property of the data item object of the outer repeater.

    If this is not possible because the objects are not constructed in this way you can still use the nested repeater approach, just you will have to bind each inner repeater in the ItemDataBound event.

    马蹄踏│碎落叶 2024-11-09 15:41:03

    答案是查德和克里斯答案的结合。我最终将转发器全部转储在一起,并在函数中使用一系列条件和循环将 html 保存在 StringBuilder() 中,并输出到 Literal 中。

    谢谢你们。

    The answer is a combination of Chad and Chris's answers. I ended up dumping the repeater all together and used a series of conditionals and loops in a function to save html in a StringBuilder() and output into a Literal.

    Thanks guys.

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