用Repeater生成一个树控件,想把子节点在后台代码中动态绑定,可是代码不能按预期的执行,请教大家帮忙分析一下是什么原因呢?
代码说明如下:
aspx页面代码:
<ul class="tree treeFolder" >
<asp:Repeater ID="partyRepeater" OnItemDataBound="partyRepeater_ItemDataBound" runat="server" >
<ItemTemplate>
<li><a href="javascript:;" id="partyLink" runat="server"></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
aspx.cs代码:
PartyEntity party = e.Item.DataItem as PartyEntity;
if (party == null)
return;
HtmlAnchor a = e.Item.FindControl("partyLink") as HtmlAnchor;
if (a != null) {
a.InnerText = party.Title.Trim();
a.Attributes.Add("onclick", "loadNode("+ party.ID +")");
}
List<PartyEntity> children = GetChildren(party.ID);
if (children == null || children.Count == 0)
return;
HtmlGenericControl ul = new HtmlGenericControl("UL");
e.Item.Controls.Add(ul);
// 绑定子节点
Repeater rep = new Repeater();
rep.ItemTemplate = new PartyTemplate();
rep.ItemDataBound += new RepeaterItemEventHandler(partyRepeater_ItemDataBound);
rep.DataSource = children;
rep.DataBind();
ul.Controls.Add(rep);
}
期望的结果:黄色高亮部分作为子节点,放在li标签内;
<LI>
<DIV >
<DIV class=node></DIV>
<DIV class=file></DIV><A id=partyLink onclick=loadNode(1) href="javascript:;" >父节点</A></DIV>
<UL >
<LI><A onclick=loadNode(2) >子节点1</A></LI>
<LI><A onclick=loadNode(3) >子节点2</A></LI>
</UL>
</LI>
</UL>
实际运行结果,总是在li标签外:
<LI>
<DIV >
<DIV class=node></DIV>
<DIV class=file></DIV><A id=partyLink onclick=loadNode(1) href="javascript:;" >
</LI>
<UL>
<LI><A onclick=loadNode(3) >子节点2</A></LI>
</UL></UL>