递归生成数CreateTree()

using Microsoft.Web.UI.WebControls;
 private void CreateTree()
  {
   try
   {
    TableRow tr = new TableRow() ;
    TableCell tc = new TableCell() ;
    System.Web.UI.HtmlControls.HtmlGenericControl div = new HtmlGenericControl() ;
    div.Style["width"] = "100%" ;
    div.Style["height"] = "477px" ;
    div.Attributes["class"] = "div-scroll" ;
    TreeView treeview = new TreeView() ;
    //产生树
    CreatMyTree(treeview);
    treeview.ID = "viewtree" ;
    //选中的菜单展开
    treeview.SelectExpands = true ;
   
    //初始化展开层数
    treeview.ExpandLevel = 3 ;
    treeview.Width =Unit.Parse("100%");
    div.Controls.Add(treeview) ;

    tc.Controls.Add(div) ;
    tr.Cells.Add(tc) ;
    tbTree.Rows.Add(tr) ;
   }
   catch(Exception err)
   {
    Common.ShowInfor(err.Message,"error",this.Page) ;
    return ;
   }
  }

  #region 生成树
  /// <summary>
  /// 生成树
  /// </summary>
  /// <param name="mytree"></param>
  /// <returns></returns>
  private TreeView CreatMyTree( TreeView mytree )
  {
   string strSql = "select * from "+strView+" where PTypeFSN is null  order by PTypeSort " ;
   DataSet dsTree = dboperReq.GetDSet(strSql) ;
   int iTreeNode = dsTree.Tables[0].Rows.Count ;
   if(iTreeNode==0)
   {
    hdValue.Value = "1" ;
   }
   for( int i = 0 ; i < iTreeNode ; i++ )
   {
    TreeNode treenode = new TreeNode() ;
    treenode.DefaultStyle["FONT-FAMILY"] = "宋体" ;
    treenode.DefaultStyle["FONT-SIZE"] = "9pt" ;
    treenode.SelectedStyle["background-color"] = "#eaf4fd" ;
    treenode.Type = dsTree.Tables[0].Rows[i]["PTypeSN"].ToString() ;
    treenode.Text = "<a href='GoodsTypeMt.aspx?PTypeSN="+treenode.Type+"' style='text-decoration:none;color:black'>"+dsTree.Tables[0].Rows[i]["PTypeSName"].ToString()+"</a>" ;
    CreatNode( treenode ) ;
    mytree.Nodes.Add(treenode) ;
    if( ViewState["PTypeSN"].ToString() == "" )
    {
     ViewState["PTypeSN"] = dsTree.Tables[0].Rows[0]["PTypeSN"].ToString() ;
    }
   }
   return mytree ;
  }

  //递归生成树的节点
  private void CreatNode( TreeNode mynode )
  {
   string strFfseq = mynode.Type ;
   string strSql = "select * from "+strView+" where PTypeFSN = '"+strFfseq+"' and flag='0' order by PTypeSort " ;
//   DbOperate dboperReq2= new DbOperate();
   DataSet dsNode = dboperReq.GetDSet(strSql) ;
   int iNodes = dsNode.Tables[0].Rows.Count ;
   for( int i = 0 ; i < iNodes ; i++ )
   {
    TreeNode subnode = new TreeNode() ;
    subnode.DefaultStyle["FONT-FAMILY"] = "宋体" ;
    subnode.DefaultStyle["FONT-SIZE"] = "9pt" ;
    subnode.HoverStyle["background-color"] = "#eaf4fd" ;
    //    string strLevel = dsNode.Tables[0].Rows[i]["fstep"].ToString() ;
    subnode.Type = dsNode.Tables[0].Rows[i]["PTypeSN"].ToString() ;
    subnode.Text = "<a href='GoodsTypeMt.aspx?PTypeSN="+subnode.Type+"' style='text-decoration:none;color:black'>"+dsNode.Tables[0].Rows[i]["Ptypeid"].ToString()+dsNode.Tables[0].Rows[i]["PTypeSName"].ToString()+"</a>" ;
    mynode.Nodes.Add(subnode) ;
    string strNodeSql = "select * from "+strView+" where PTypeSN = '"+subnode.Type+"'" ;
//    DbOperate dboperReq1= new DbOperate();
    DataSet dsLeaf = dboperReq.GetDSet(strNodeSql) ;
    if( dsLeaf != null && dsLeaf.Tables[0].Rows.Count > 0 )
    {
     CreatNode(subnode) ;
    }
   }
  }
  #endregion

posted on 2009-07-30 10:33  jameshappy  阅读(487)  评论(0编辑  收藏  举报