如何绘制迭代ul-li列表模式
本文关键字:迭代 ul-li 列表 模式 绘制 何绘制 | 更新日期: 2023-09-27 18:28:25
我想在预览中绘制Html。
要查看预览,请单击此处。查看页面的源代码以查看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);
这个代码有什么错误?
试试这个:
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;
}
编辑:
您应该将您的输入表示为树结构。在这里,我使用了一个具有ID
和Children
属性的Node
对象:
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);
}
}
试试这个
HtmlGenericControl Initial = new HtmlGenericControl("ul");
DrawTree(ref Initial,4);
now方法
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);
}
}
问题很简单
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();
的位置但仍然不知道为什么会这样