生成Treeview树 递归方法

产生树的效果图
生成Treeview树 递归方法

aspx页面

< asp:TreeView ID = " tv "  runat = " server "  ImageSet = " Arrows "  LineImagesFolder = " ~/TreeLineImages "  OnSelectedNodeChanged = " tv_SelectedNodeChanged "  OnTreeNodeCheckChanged = " tv_TreeNodeCheckChanged "  OnTreeNodeCollapsed = " tv_TreeNodeCollapsed "  OnTreeNodeDataBound = " tv_TreeNodeDataBound "  OnTreeNodeExpanded = " tv_TreeNodeExpanded "  OnTreeNodePopulate = " tv_TreeNodePopulate " >
                
< ParentNodeStyle Font - Bold = " False "   />
                
< HoverNodeStyle Font - Underline = " True "  ForeColor = " #5555DD "   />
                
< SelectedNodeStyle Font - Underline = " True "  ForeColor = " #5555DD "  HorizontalPadding = " 0px "
                    VerticalPadding
= " 0px "   />
                
< NodeStyle Font - Names = " Verdana "  Font - Size = " 8pt "  ForeColor = " Black "  HorizontalPadding = " 5px "
                    NodeSpacing
= " 0px "  VerticalPadding = " 0px "   />
            
</ asp:TreeView >
生成树的递归方法

public   void  RecursionUser( string  strUserID, TreeNodeCollection NodeParent)
    
{
        DataView dv 
= new DataView(myDataSet.Tables[0]);
        dv.Sort 
= "LEVEL_CODE";
        dv.RowFilter 
= "PUSER_ID='" + strUserID + "'";

        TreeNode tempNode;
        
for (int i = 0; i < dv.Count; i++)
        
{
            tempNode 
= new TreeNode();
            tempNode.Text 
= dv[i]["ACCOUNT"].ToString();//要显示的值
            tempNode.Value = dv[i]["USER_ID"].ToString();//Node的值
            tempNode.Selected = false;
            tempNode.Checked 
= true;//这个是当有checkbox时设置
            tempNode.Collapse();

           
// NodeParent.ChildNodes.Add(tempNode);
           NodeParent.Add(tempNode);

            RecursionUser(dv[i][
"USER_ID"].ToString(), tempNode.ChildNodes);

        }



    }
调用递归方法 生成树
   RecursionUser(NULL_GUID, tv.Nodes);

你可能感兴趣的:(treeview)