3.10 向 Default.aspx 页面上添加 TreeView 控件:
<asp:TreeView ID="trvLocation" runat="server" Font-Size="12px"
ShowLines="True">
</asp:TreeView>
3.11 在 Default.aspx 页面后置代码中编写 BindData 数据绑定方法:
private void BindData()
{
DAO dao = new DAO();
LocationCollection locs = dao.GetLocations();
TreeNodeCollection nodes = CreateTreeNodes(locs);
foreach (TreeNode node in nodes)
{
trvLocation.Nodes.Add(node);
}
}
3.12 BindData 方法调用了 CreateTreeNode 方法返回节点集合,该方法中递归调用自身以得到全部所在地节点:
private TreeNodeCollection CreateTreeNodes(LocationCollection locs)
{
TreeNodeCollection nodeColl = new TreeNodeCollection();
foreach (Location loc in locs)
{
TreeNode node = new TreeNode(loc.Name, loc.Id.ToString());
if (loc.SubLocations.Count > 0)
{
TreeNodeCollection subColl = CreateTreeNodes(loc.SubLocations);
foreach (TreeNode subNode in subColl)
node.ChildNodes.Add(subNode);
}
nodeColl.Add(node);
}
return nodeColl;
}
本文来源:不详 作者:佚名