程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> TreeView生成漂亮的樹

TreeView生成漂亮的樹

編輯:.NET實例教程
namespace ut.include
...{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClIEnt;
    using System.Data.OleDb;
    using Microsoft.Web.UI.WebControls;
    
    //using com.utils;

    /**//// <summary>
    ///        UserknowledgeTree 的摘要說明。
    /// </summary>
    public class UserknowledgeTree : System.Web.UI.UserControl
    ...{
        protected Microsoft.Web.UI.WebControls.TreeVIEw tvTest;
        //聲明控件
        private TreeNode TreeNodeAll=null;
        private TreeNode TreeNode1=null;
        private TreeNode TreeNode2=null;
        private TreeNode TreeNode22=null;
        private TreeNode TreeNode3=null;
        private OleDbConnection conn=null;
        private OleDbDataAdapter sda=null;
        private DataSet ds=null;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
        private  string strSql="";
        private void Page_Load(object sender, System.EventArgs e)
        ...{
            if(!Page.IsPostBack)
            ...{
                BindTreeVIEw();
            }
        }
        private void   BindTreeVIEw()
        ...{
            //讀出第總層
            string SqlConnectionString =System.Configuration.ConfigurationSettings.APPSettings["SQLCONNECTIONSTRING"].ToString();
            DataSet dsAll=new DataSet();
       &nb SqlConnection conn=new  SqlConnection(SqlConnectionString);

            strSql=getDocTypeList(1);
            SqlDataAdapter sdaAll=new  SqlDataAdapter (strSql,conn);
            sdaAll.Fill(dsAll,"DocumentType");

            for( int i = 0; i < dsAll.Tables[ "DocumentType" ].Rows.Count; i++  )
            ...{
            
                TreeNodeAll = new TreeNode( );
                TreeNodeAll.Text = dsAll.Tables[ "DocumentType" ].Rows[ i ][ "docTypeName" ].ToString();
                TreeNodeAll.ID=dsAll.Tables[ "DocumentType" ].Rows[ i ][ "docTypeid" ].ToString();
                
                TreeNodeAll.ExpandedImageUrl="..\images\open.gif";
                TreeNodeAll.ImageUrl="..\images\dot3.gif";
                TreeNodeAll.NavigateUrl="..\DocAdvanceSearch1.ASPx?DocTypeID="+TreeNodeAll.ID+"&DocTypeName="+TreeNodeAll.Text;
                TreeNodeAll.Target="main";
                //如果是產品手冊就顯示樹形結構
                if(TreeNodeAll.Text=="產品手冊")
                ...{
                    BindTreeVIEw2();
                }
                this.tvTest.Nodes.Add(TreeNodeAll);
            }
             
        }
        private void BindTreeVIEw2()
        ...{
            //遠程調用得到一個DATASET
            //在DATASET中放入FLOD,FILE二張表
            ds=BackDataSet();
            DataTable dt=ds.Tables["Fold"];

            //把File表中的第一層放入Flod表中
            for(int i=0;i<ds.Tables["File"].Rows.Count;i++)
            ...{
                //如果FolderParentID為0則說明是第一層
                if(ds.Tables["File"].Rows[i]["FileFolderID"].ToString().Trim()=="0")
                ...{
                   DataRow dr=dt.NewRow();
                    dr["FolderID"]=ds.Tables["File"].Rows[ i ][ "FileID" ].ToString( );
                    dr["FolderParentID"]=ds.Tables["File"].Rows[ i ][ "FileFolderID" ].ToString( );
                    dr["FolderName"]=ds.Tables["File"].Rows[ i ][ "FileName" ].ToString( );
                    dr["FolderDescription"]=ds.Tables["File"].Rows[ i ][ "FileDescription" ].ToString( );
                    dt.Rows.Add(dr);
                
                }
            
            }
        
        
            //讀出Fold表和File表中的第一層
            for(int i=0;i<ds.Tables["Fold"].Rows.Count;i++)
            ...{
                //如果FolderParentID為0則說明是第一層
                if(ds.Tables["Fold"].Rows[i]["FolderParentID"].ToString().Trim()=="0")
                ...{
                    TreeNode1=new TreeNode();
 &            TreeNode1.Text = ds.Tables[ "Fold" ].Rows[ i ][ "FolderName" ].ToString( );
                    TreeNode1.ExpandedImageUrl="..\images\open.gif";
                    
                    string foldName=ds.Tables[ "Fold" ].Rows[ i ][ "FolderName" ].ToString().Trim();
                    if(foldName=="OMUX" || foldName=="iPAS" || foldName=="Terminal" )
                    ...{
                        TreeNode1.ImageUrl="..\images\file.gif";
                        TreeNode1.NavigateUrl="..\DocAdvanceSearch1.ASPx?ProList=1&sys="+ds.Tables[ "Fold" ].Rows[ i ][ "FolderDescription" ].ToString( );
                        TreeNode1.Target="main";
                    }
                    else
                    ...{
                        TreeNode1.ImageUrl="..\images\close.gif";

                    }

                    //取第二層數據
                 &nbs    二層遍歷
                    for( int j = 0; j < ds.Tables[ "Fold" ].Rows.Count; j++ )
                    ...{
                        //假如FolderParentID與FolderID相一致說明是同一級中數據,將此節點加入到此節中
                        string folderID=ds.Tables[ "Fold" ].Rows[ i ][ "FolderID" ].ToString().Trim();
                        string folderParentID=ds.Tables[ "Fold" ].Rows[ j ][ "FolderParentID" ].ToString().Trim();
                        
                        if(folderID==folderParentID)
                        ...{
                            TreeNode2 = new TreeNode( );
                            TreeNode2.Text = ds.Tables[ "Fold" ].Rows[j][ "FolderName" ].ToString( );
                            TreeNode2.ExpandedImageUrl="..\images\open.gif";
                            TreeNode2.ImageUrl="..\images\close.gif";
                           

                            //第三層遍歷
                             for( int k = 0; k< ds.Tables[ "file" ].Rows.Count; k++ )
                            ...{
                                //假如FolderParentID與FolderID相一致說明是同一級中數據,將此節點加入到此節中
                                string folderID2=ds.Tables[ "Fold" ].Rows[ j ][ "FolderID" ].ToString().Trim();
                                string fileFolderID2=ds.Tables[ "File" ].Rows[ k ][ "FileFolderID" ].ToString().Trim();
                                if(folderID2==fileFolderID2)
                                ...{
                                    TreeNode3 = new TreeNode( );
                                    TreeNode3.Text = ds.Tables[ "file" ].Rows[ k ][ "FileName" ].ToString( );
                                    TreeNode3.ImageUrl="..\images\file.gif";
                                    TreeNode3.NavigateUrl="..\DocAdvanceSearch1.ASPx?ProList=1&sys="+ds.Tables[ "file" ].Rows[ k ][ "FileDescription" ].ToString( );
                                    TreeNode3.Target="main";
                                         TreeNode2.Nodes.Add( TreeNode3 );
                                    
                                }
                            
                            }
                            TreeNode1.Nodes.Add(TreeNode2 );
                        }
                       
                    }
                    //FILE表中第二層遍歷
                    for( int j = 0; j < ds.Tables[ "File" ].Rows.Count; j++ )
                    ...{
                        //假如FolderParentID與FolderID相一致說明是同一級中數據,將此節點加入到此節中
                        string folderID=ds.Tables[ "Fold" ].Rows[ i ][ "FolderID" ].ToString().Trim();
                        string fileFolderID=ds.Tables[ "File" ].Rows[ j ][ "FileFolderID" ].ToString().Trim();
                        
                        if(folderID==fileFolderID)
                             ...{
                            TreeNode22 = new TreeNode( );
                            TreeNode22.Text = ds.Tables[ "File" ].Rows[ j ][ "FileName" ].ToString( );
                            TreeNode22.ImageUrl="..\images\file.gif";
                            TreeNode22.NavigateUrl="..\DocAdvanceSearch1.ASPx?ProList=1&sys="+ds.Tables[ "file" ].Rows[ j ][ "FileDescription" ].ToString( );
                            TreeNode22.Target="main";

                            TreeNode1.Nodes.Add( TreeNode22 );
                        }
                    }
                   TreeNodeAll.Nodes.Add(TreeNode1);

                }
            
            }
        
            
        }

        //返回DataSet
        private DataSet  BackDataSet()
        ...{
                      //查看配置文件如果Remoting為false就讀取本地文件
            string isRemoting=System.Configuration.ConfigurationSettings.APPSettings["isRemoting"].ToString().Trim();
            DataSet ds=new DataSet();
            if(isRemoting.ToUpper()=="FALSE")
            ...{
                //讀出Flod表
                string conStr=System.Configuration.ConfigurationSettings.APPSettings["irstrconn"].ToString();
                
                strSql="Select FolderID,FolderParentID,FolderName,FolderDescription from Folder";
                conn=new OleDbConnection(conStr);
                sda=new OleDbDataAdapter(strSql,conn);
                sda.Fill(ds,"Fold");
                //讀出File表
                strSql="Select FileID,FileFolderID,FileName,FileDescription from File";
                sda=new OleDbDataAdapter(strSql,conn);
                sda.Fill(ds, "File");
                
            }
            else
            ...{                    string catheName="DocTree";
                if(Cache[catheName]==null)
                ...{
                    com.ut.ui.RemoServer rs=new com.ut.ui.RemoServer();
                    ds= rs.GetDocTree();
                    Cache.Add(catheName, ds, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero,System.Web.Caching.CacheItemPriority.High,null);
                    
                }
                else
                ...{
                    
                    ds=(DataSet)Cache[catheName];
                }
                
            }

            return ds;
            
        }



        public stringgetDocTypeList(int status)
        ...{
            String sql = "";
            if(status < 2)
            ...{
                sql = "select docTypeid,docTypeName from DocumentType where isOutPublish=" + status + " order by orderid";
            }
            else if(status == 2)
            ...{
                sql = "select docTypeid,docTypeName from DocumentType order by orderid";
            }
            else if(status > 2)
            ...{
                sql = "select docTypeid,docTypeName from DocumentType where isInnerPublish=1 order by orderid";
                  }
            return sql;
        }
        Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
        override protected void OnInit(EventArgs e)
        ...{        ...{
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        
    }
}

            //
            // CODEGEN: 該調用是 ASP.Net Web 窗體設計器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /**//// <summary>
        ///        設計器支持所需的方法 - 不要使用代碼編輯器
        ///        修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        ...{
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved