如何绘制迭代的ul-li List模式?

时间:2013-08-21 13:34:52

标签: c# asp.net

我想在预览中绘制Html。

查看预览click here。查看页面的Source以查看ul-li呈现。 为此,我写了一个如下所示的方法

private void DrawTree(HtmlGenericControl ParentULTag, int count)
    {

        for (int i = 0; i < count; i++)
        {
            HtmlGenericControl ChildLi = new HtmlGenericControl("li");
            HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
            ChildLi.Controls.Add(ChildUl);
            ParentULTag.Controls.Add(ChildLi);
            ChildLi.InnerText = i.ToString();
            DrawTree(ChildUl, i);
        }
    }

但它像这样画出来

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

我在页面加载中启动了该方法,如图所示

HtmlGenericControl Initial = new HtmlGenericControl("ul"); 
DrawTree(Initial,4);

这段代码中的错误是什么?

3 个答案:

答案 0 :(得分:3)

试试这个:

    private static HtmlGenericControl GenerateList(int count)
    {
        HtmlGenericControl parent = new HtmlGenericControl("ul");
        for (int i = 1; i <= count; i++)
        {
            HtmlGenericControl ChildLi = new HtmlGenericControl("li");
            ChildLi.InnerText = i.ToString();
            HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
            ChildLi.Controls.Add(ChildUl);
            for (int j = 1; j <= i; j++) {
                HtmlGenericControl FinalLi = new HtmlGenericControl("li");
                FinalLi.InnerText = j.ToString();
                ChildUl.Controls.Add(FinalLi);
            }
            parent.Controls.Add(ChildLi);
        }
        return parent;
    }

编辑:

您应该将输入表示为树结构。在这里,我使用了具有NodeID属性的Children对象:

class Node
{
    public int ID { get; set; }
    public Node[] Children { get; set; }
    public Node(int id, Node[] children)
    {
        ID = id;
        Children = children;
    }
    public Node(int id)
    {
        ID = id;
        Children = new Node[] { };
    }
}

class Program
{

    public static HtmlGenericControl GetList(Node root)
    {
        HtmlGenericControl ul = new HtmlGenericControl("ul");
        GetListImpl(root, ul);
        return ul;
    }

    private static void GetListImpl(Node root, HtmlGenericControl rootElement)
    {
        foreach (var item in root.Children)
        {
            HtmlGenericControl li = new HtmlGenericControl("li");
            li.InnerText = item.ID.ToString();
            if (item.Children.Length > 0)
                li.Controls.Add(GetList(item));

            rootElement.Controls.Add(li);
        }
    }

    public static string HtmlGenericControlToString(HtmlGenericControl control)
    {
        string contents = null;
        using (System.IO.StringWriter swriter = new System.IO.StringWriter())
        {
            HtmlTextWriter writer = new HtmlTextWriter(swriter);
            control.RenderControl(writer);
            contents = swriter.ToString();
        }
        return contents;
    }


    static void Main(string[] args)
    {
        Node root = new Node(0,
            new Node[]{
                new Node(1, new Node[]{
                    new Node(1),
                    new Node(2)
                }),
                 new Node(2, new Node[]{
                    new Node(1),
                    new Node(2, new Node[]{
                        new Node(1)
                    })
                })
            });
        var ul = GetList(root);
        var html = HtmlGenericControlToString(ul);
        Console.WriteLine(html);
    }
}

答案 1 :(得分:1)

试试这个

HtmlGenericControl Initial = new HtmlGenericControl("ul"); 
DrawTree(ref Initial,4);

现在方法

private void DrawTree(ref HtmlGenericControl ParentULTag, int count)
    {

        for (int i = 0; i < count; i++)
        {
            HtmlGenericControl ChildLi = new HtmlGenericControl("li");
            HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
            ChildLi.Controls.Add(ChildUl);
            ParentULTag.Controls.Add(ChildLi);
            ChildLi.InnerText = i.ToString();
            DrawTree(ref ChildUl, i);
        }
    }

答案 2 :(得分:0)

问题很简单

 private void DrawTree(HtmlGenericControl ParentULTag, int count)
    {

        for (int i = 0; i < count; i++)
        {
            HtmlGenericControl ChildLi = new HtmlGenericControl("li");
            HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
            ChildLi.InnerText = i.ToString();
            ChildLi.Controls.Add(ChildUl);
            DrawTree(ChildUl, i);
            ParentULTag.Controls.Add(ChildLi);
        }
    }

请注意ChildLi.InnerText = i.ToString();行的位置  但仍然不知道为什么会如此