那里。我在一个网页上有一个 ul,我从后面的代码中填充了一个我在树中组织的数据库。我可以很好地从数据库中获取值并将它们完全列出,但是我希望子节点出现在每个父节点下的内部列表中。这是我当前的代码
protected void createMenu(TreeView navTree) {
foreach(TreeNode node in navTree.Nodes){
HtmlGenericControl li = new HtmlGenericControl("li");
navMenu.Controls.Add(li);
HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "");
anchor.Attributes.Add("class", "toggle");
anchor.Attributes.Add("onClick", "navButton");
anchor.InnerText = node.Text;
li.Controls.Add(anchor);
foreach (TreeNode childNode in node.ChildNodes) {
HtmlGenericControl child_li = new HtmlGenericControl("li");
navMenu.Controls.Add(child_li);
HtmlGenericControl childAnchor = new HtmlGenericControl("a");
childAnchor.Attributes.Add("href", "");
childAnchor.Attributes.Add("onClick", "childButton");
childAnchor.InnerText = childNode.Text;
child_li.Controls.Add(childAnchor);
}
}
}
这是我要重新创建的 html:
<div id="sidebar">
<nav>
<h2>Sites</h2>
<ul>
<li class="toggle"><a href="#">Akron</a></li>
<li class="toggle"><a href="#">Monterey</a></li>
<li class="toggle is-open"><a href="#">Sacramento</a>
<ul class="subnav">
<li><a class="active" href="#"><span class="quantity">5</span> George</a></li>
<li><a href="#"><span class="quantity">7</span> John</a></li>
<li><a href="#"><span class="quantity">18</span> Bob</a></li>
</ul>
</li>
<li class="toggle"><a href="#">Stanislaus</a></li>
</ul>
</nav>