【问题标题】:Rendering an unordered list using asp.net使用 asp.net 渲染无序列表
【发布时间】:2020-01-29 14:06:51
【问题描述】:

我需要使用asp.net/C#从数据库中获得的数据呈现一个无序列表。我的数据看起来像这样。

id     Name               Depth

1    ELECTRONICS             0

2    TELEVISIONS             1

3    Tube                   2

4    LCD                    2

5    Plasma                 2

6   Portable electronics    1

7   MP3 Player              2

8    Flash                  3

9    CD Players             2

10    2 Way Radio           2

使用上面的示例数据,我需要根据深度呈现一个无序列表,格式如下

<ul>
  <li>ELECTRONICS

<ul>

   <li>TELEVISIONS

  <ul>

    <li>TUBE</li>

    <li>LCD</li>

    <li>PLASMA</li>

  </ul>

   </li>

   <li>PORTABLE ELECTRONICS
  <ul>

    <li>MP3 PLAYERS

   <ul>

<li>FLASH</li>

   </ul>

    </li>

    <li>CD PLAYERS</li>

    <li>2 WAY RADIOS</li>

  </ul>

   </li>

</ul>

</li>

</ul>

以上数据只是一个示例,我有一个巨大的记录集,必须转换为无序列表。有人可以告诉我如何实现这一点吗?

更新: 我已将生成无序列表的代码更新为以下内容。

int lastDepth = -1;
        int numUL = 0;

        StringBuilder output = new StringBuilder();


        foreach (DataRow row in ds.Tables[0].Rows)
        {

            int currentDepth = Convert.ToInt32(row["Depth"]);

            if (lastDepth < currentDepth)
            {
                if (currentDepth == 0)
                {
                    output.Append("<ul class=\"simpleTree\">");
                    output.AppendFormat("<li class=\"root\"><span><a href=\"#\" title=\"root\">root</a></span><ul><li class=\"open\" ><span><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
                }
                else
                {
                    output.Append("<ul>");
                    if(currentDepth==1)
                    output.AppendFormat("<li><span>{0}</span>", row["name"]);
                    else
                        output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
                }
                numUL++;
            }
            else if (lastDepth > currentDepth)
            {
                output.Append("</li></ul></li>");
                if(currentDepth==1)
                output.AppendFormat("<li><span>{0}</span>", row["name"]);
                else
                    output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"], row["id"]);
                numUL--;
            }
            else if (lastDepth > -1)
            {
                output.Append("</li>");
                output.AppendFormat("<li><span class=\"text\"><a href=\"#\" title={1}>{0}</a></span>", row["name"],row["id"]);
            }


            lastDepth = currentDepth;
        }

        for (int i = 1; i <= numUL+1; i++)
        {
            output.Append("</li></ul>");
        }

使用上面的代码,我的无序列表看起来像这样。

<ul class="simpleTree">
<li class="root">
<span><a href="#" title="root">root</a></span>
<ul>
<li class="open" >
<span><a href="#" title=1>ELECTRONICS</a></span>
<ul>
<li>
<span>TELEVISIONS</span>
<ul>
<li>
<span class="text"><a href="#" title=3>TUBE</a></span>
</li>
<li>
<span class="text"><a href="#" title=4>LCD</a></span>
</li>
<li><span class="text"><a href="#" title=5>PLASMA</a></span>
</li>
</ul>
</li>
<li>
<span>PORTABLE ELECTRONICS</span>
<ul>
<li>
<span class="text"><a href="#" title=7>MP3 PLAYERS</a></span>
<ul>
<li>
<span class="text"><a href="#" title=8>FLASH</a></span>
</li>
</ul>
</li>
<li>
<span class="text"><a href="#" title=9>CD PLAYERS</a></span>
</li>
<li>
<span class="text"><a href="#" title=10>2 WAY RADIOS</a></span>
</li>
</ul>
</li></ul>
</li></ul>
</li></ul>

谢谢。

【问题讨论】:

    标签: asp.net html-lists


    【解决方案1】:

    您可以在代码中执行类似的操作,然后将结果输出到页面上的 Literal 控件:

    int lastDepth = -1;
    int numUL = 0;
    
    StringBuilder output = new StringBuilder();
    
    
    foreach (DataRow row in yourDataTable.Rows) {
    
        int currentDepth = row["Depth"];
    
        if (lastDepth < currentDepth) {
            output.append("<ul>");
            numUL++
        }
        else if (lastDepth > currentDepth) {
            output.append("</li></ul></li>");
            numUL--
        }
        else if (lastDepth > -1) {
            output.append("</li>");
        }
    
        output.appendformat("<li class=\"depth-{1}\">{0}", row["name"], currentDepth);
    
        lastDepth = currentDepth;
    }
    
    for (int i = 1;i <= numUL;i++)
    {
        output.append("</li></ul>");
    }
    
    
    
    yourLiteralControl.Text = output.toString();
    

    更新:我这样做是为了让它在与 cmets 中要求的深度相关的列表项上放入一个 css 类。

    【讨论】:

    • 我真的很想 DV 这个,但这是一个技术上正确的声明,但我绝对不建议这样做。有为此目的的控件,我宁愿在此之前在 aspx 页面中绑定一个 ForEach 循环。
    • 嗨,克里斯,你为什么不推荐上述方法?
    • 很想听听为什么你认为这与一堆嵌套的 ListView 相比如此糟糕(考虑到灵活的数量,我不确定在没有大量额外代码的情况下甚至可以在这种情况下工作)对他的名单有深度)。不管你把这个 foreach 循环放在后面的代码中还是放在 ASPX 页面本身中,都没有区别,除非你不缓冲页面的输出。
    • 因为这样的代码基本上是不可维护的。如果他需要嵌套列表,您可以在 ListView 内呈现 ListViews。 ListView 除了更容易扩展之外,还提供了包含事件支持的能力,当然真正的数据绑定是一个巨大的优势。
    • 我之前已经用一个呈现 ListView 的 ListView 实现了这个确切的场景,然后我将父 listview 绑定到一个 List 列表,它就完成了。
    【解决方案2】:

    使用中继器控件。

    样品:

    MSDN Documentation

    编辑:没有注意到关于深度的部分,改用Treeview Control,看看关于绑定到数据库的部分。

    【讨论】:

    • Repeater 控件几乎已被 ListView 控件弃用。
    • 嗨 Barrett,一旦我以树视图的形式呈现无序列表,我需要应用一些 js 为其提供拖放功能。如果我使用树视图控件,这可能吗?此外,我在 db 中有大约 500 条记录。如果我将它们呈现为树视图,这不会成为服务器的负载并在回发之间造成延迟吗?
    • @kranthi:对于拖放和丰富的功能,您可以尝试第三方组件,例如 Telerik telerik.com/products/aspnet-ajax/treeview.aspx
    【解决方案3】:

    除非您使用的是 .NET 2.0,否则您应该使用 ListView 控件。

    Using ASP.NET 3.5's ListView and DataPager Controls: Displaying Data with the ListView

    【讨论】:

      【解决方案4】:
           void PopulateChild(int ParentId, StringBuilder output)
                      {
                          var childs = GetChildren(ParentId); //get all the chilren with respect to that parent
                          if (childs.Count > 0)
                          {
      //if children are there append another list to the parent node
                              output.Append("<ul>");
                              childs.ForEach(x =>
                              {
                                  if (GetChildren(x.Id).Count > 0)
                                  {
                                      output.AppendFormat("<li><a href=\"#\">{0}</a>", x.Email);
                                      output.Append("<ul>");
                                      PopulateChild(x.Id, output);
                                      output.Append("</li>");
                                      output.Append("</ul>");
      
                                  }
                                  else
                                      output.AppendFormat("<li><a href=\"#\">{0}</a></li>", x.Email);
      
                              });
                          }
      
                          output.Append("</ul>");
                      }
      
          //call this method with the parentid 
          private void PopulateList(int CustomerId)
                      {
                          StringBuilder output = new StringBuilder();
                          var row = GetCustomerDetailById(CustomerId);
                          output.Append("<ul>");
                          output.AppendFormat("<li><a href=\"#\">{0}</a>", row.Email);
      
                          PopulateChild(CustomerId, output);
      
                          output.Append("</li></ul>");
      
                          Literal1.Text = output.ToString();
                      }
      

      【讨论】:

      • 您好,欢迎来到 SO。这是您编写的大量代码,但如果没有任何 cmets,它就不太容易理解。如果您在此答案中附加一些关于您的代码如何工作以及它如何解决 OP 问题的句子,这将对其他用户非常有帮助~
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2019-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多