前台:
<asp:TreeView ID="TreeView1" runat="server" Font-Names="宋体" Font-Size="14px" CollapseImageUrl="~/images/LI.GIF" ExpandDepth="0" ExpandImageUrl="~/images/LI.GIF" NoExpandImageUrl="~/images/LI.GIF" Width="160px" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
<HoverNodeStyle Font-Size="13px" />
<LeafNodeStyle Font-Size="13px" />
<NodeStyle BorderStyle="None" HorizontalPadding="7px" NodeSpacing="5px" VerticalPadding="5px" />
</asp:TreeView>
代码:(提示:TreeView1是TreeView控件的id;tabC.GetTabsByParentId(67)为根节点的信息,返回值是ArrayList类型;TabInfo中包含根节点的text(info.TabName)和value(info.TabID)的值;)。
protected void Page_Load(System.Object sender, System.EventArgse) {
if (!Page.IsPostBack)
{
Binder2();
}
}
///<summary>
///绑定树
///</summary>
publicvoid Binder2()
{
TreeView1.Nodes.Clear();
TreeNodenode;
foreach(TabInfo info intabC.GetTabsByParentId(67))
{
node = new TreeNode(info.TabName,info.TabID.ToString());
node.Expand();
this.TreeView1.Nodes.Add(node);
BinderChildNodes2(node, info.TabID);
if(node.ChildNodes.Count > 0)
node.SelectAction = TreeNodeSelectAction.Expand;
}
}
///<summary>
///绑定叶子子节点
///</summary>
///<paramname="node"></param>
///<param name="ParentID"></param>
privatevoid BinderChildNodes2(TreeNodenode, int ParentID)
{
TreeNoden;
foreach(TabInfo Info intabC.GetTabsByParentId(ParentID))
{
n = new TreeNode(Info.TabName,Info.TabID.ToString());
//BinderChildNodes2(n, Info.TabID);
//n.Expand();
n.SelectAction = TreeNodeSelectAction.Select;
node.ChildNodes.Add(n);
}
}