2

我想在预览中绘制 Html。

要查看预览,请单击此处。View 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);

这段代码有什么错误?

4

3 回答 3

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;
    }

编辑:

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

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);
    }
}
于 2013-08-21T13:50:53.000 回答
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);
        }
    }
于 2013-08-21T13:38:13.083 回答
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(); 但仍然不知道为什么会这样

于 2013-08-22T07:47:55.157 回答