无限增长的Tree-----查找

 

数据表设计:

IF OBJECT_ID ('dbo.WapClass') IS NOT NULL
 DROP TABLE dbo.WapClass
GO

CREATE TABLE dbo.WapClass
 (
 ClassId       INT IDENTITY NOT NULL, --编号
 ClassParentId INT NOT NULL,   --父节点编号
 ClassName     NVARCHAR (50) NOT NULL,  --节点名称
 Url           NVARCHAR (300) NULL,  --点击后连接的路径
 CONSTRAINT PK_WapClass PRIMARY KEY (ClassId)
 )
GO

 

 实体类:WapBar.Model.WapClass.cs

using System;
namespace WapBar.Model
{
 /// <summary>
 /// 实体类WapClass 

 /// </summary>
 [Serializable]
 public class WapClass
 {
  public WapClass()
  {}
        #region Model
        private int _classid;
        private string _classname;
        private int _classparentid;
        private string _url;
        /// <summary>
        ///
        /// </summary>
        public int ClassId
        {
            set { _classid = value; }
            get { return _classid; }
        }
        /// <summary>
        ///
        /// </summary>
        public string ClassName
        {
            set { _classname = value; }
            get { return _classname; }
        }
        /// <summary>
        ///
        /// </summary>
        public int ClassParentId
        {
            set { _classparentid = value; }
            get { return _classparentid; }
        }
        /// <summary>
        ///
        /// </summary>
        public string Url
        {
            set { _url = value; }
            get { return _url; }
        }
        #endregion Model

 }
}


 

aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WapClass.aspx.cs" Inherits="WapBarAdmin.WapClass" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows">
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

 

CodeBehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.Common;

namespace WapBarAdmin
{
    public partial class WapClass : System.Web.UI.Page
    {
        //数据库操纵类工厂
        private DbProviderFactory dbFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                WapBar.Model.WapClass wapClass = null;
                TreeNode treeNode = null;
                DataTable dt = this.GetTreeParentNodes();

                foreach (DataRow row in dt.Rows)
                {
                    wapClass= new WapBar.Model.WapClass();
                    wapClass.ClassId = int.Parse(row["ClassId"].ToString());
                    wapClass.ClassName = row["ClassName"].ToString();
                    wapClass.ClassParentId = int.Parse(row["ClassParentID"].ToString());
                    wapClass.Url = row["Url"].ToString();

                    treeNode = new TreeNode();
                    treeNode.Text = wapClass.ClassName;
                    treeNode.ToolTip = wapClass.ClassName;
                    treeNode.Value = wapClass.ClassId.ToString();
                    treeNode.NavigateUrl = wapClass.Url;

                    this.BindTreeNodes(wapClass.ClassId, treeNode);
                    this.TreeView1.Nodes.Add(treeNode);
                }

            }
        }

        #region 绑定子节点
        /// <summary>
        /// 绑定子节点
        /// </summary>
        /// <param name="classID">父节点ID</param>
        public void BindTreeNodes(int classID, TreeNode treeNode)
        {
            WapBar.Model.WapClass wapClass = null;
            TreeNode tempTreeNode = null;
            DataTable dt = this.GetTreeNodesByClassID(classID);
            if (dt != null && dt.Rows.Count != 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    wapClass = new WapBar.Model.WapClass();
                    wapClass.ClassId = int.Parse(row["ClassId"].ToString());
                    wapClass.ClassName = row["ClassName"].ToString();
                    wapClass.ClassParentId = int.Parse(row["ClassParentID"].ToString());
                    wapClass.Url = row["Url"].ToString();

                    tempTreeNode = new TreeNode();
                    tempTreeNode.Text = wapClass.ClassName;
                    tempTreeNode.ToolTip = wapClass.ClassName;
                    tempTreeNode.Value = wapClass.ClassId.ToString();
                    tempTreeNode.NavigateUrl = wapClass.Url;

                    treeNode.ChildNodes.Add(tempTreeNode);

                    //递归调用
                    this.BindTreeNodes(wapClass.ClassId, tempTreeNode);
                }
            }
        }
        #endregion 绑定子节点

        #region 得到连接对象
        /// <summary>
        /// 得到连接对象
        /// </summary>
        /// <returns></returns>
        public DbConnection GetDBConn()
        {
            DbConnection dbconn = null;
            try
            {
                dbconn = dbFactory.CreateConnection();
                dbconn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message.ToString());
            }
            return dbconn;
        }
        #endregion 得到连接对象

        #region 打开连接
        /// <summary>
        /// 打开连接
        /// </summary>
        /// <param name="dbconn">要打开的连接对象</param>
        public void OpenDBConn(DbConnection dbconn)
        {
            try
            {
                if (dbconn != null && dbconn.State.ToString().Equals("Closed"))
                {
                    dbconn.Open();
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message.ToString());
            }
        }
        #endregion 打开连接

        #region 关闭连接
        /// <summary>
        /// 关闭连接
        /// </summary>
        /// <param name="dbconn">关闭的连接对象</param>
        public void CloseDBConn(DbConnection dbconn)
        {
            try
            {
                if (dbconn != null && dbconn.State.ToString().Equals("Open"))
                {
                    dbconn.Close();
                }
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message.ToString());
            }
        }
        #endregion 关闭连接

        #region 查找根节点
        /// <summary>
        /// 查找根节点
        /// </summary>
        /// <returns></returns>
        public DataTable GetTreeParentNodes()
        {
            DbConnection dbconn = null;
            DbDataAdapter da = null;
            DbCommand cmd = null;
            DataTable dt = null;
            try
            {
                dt = new DataTable();

                dbconn = this.GetDBConn();
                this.OpenDBConn(dbconn);

                da = dbFactory.CreateDataAdapter();

                cmd = dbFactory.CreateCommand();
                cmd.CommandText = "select * from [WapClass] Where [ClassParentID] = 0";
                cmd.Connection = dbconn;

                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message.ToString());
            }
            finally
            {
                this.CloseDBConn(dbconn);
            }
            return dt;
        }
        #endregion 查找根节点

        #region 根据父节点的ClassID查找子节点
        /// <summary>
        ///  根据父节点的ClassID查找子节点
        /// </summary>
        /// <param name="classID">父节点ID</param>
        /// <returns></returns>
        public DataTable GetTreeNodesByClassID(int classID)
        {
            DbConnection dbconn = null;
            DbDataAdapter da = null;
            DbCommand cmd = null;
            DbParameter dbparam = null;
            DataTable dt = null;
            try
            {
                dt = new DataTable();

                dbconn = this.GetDBConn();
                this.OpenDBConn(dbconn);

                da = dbFactory.CreateDataAdapter();

                cmd = dbFactory.CreateCommand();
                cmd.CommandText = "select * from [WapClass] Where [ClassParentID]=@ClassID";
                cmd.Connection = dbconn;

                dbparam = dbFactory.CreateParameter();
                dbparam.DbType = DbType.Int32;
                dbparam.ParameterName = "@ClassID";
                dbparam.Value = classID;

                cmd.Parameters.Add(dbparam);

                da.SelectCommand = cmd;
                da.Fill(dt);
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message.ToString());
            }
            finally
            {
                this.CloseDBConn(dbconn);
            }
            return dt;
        }
        #endregion 根据父节点的ClassID查找子节点
    }
}

你可能感兴趣的:(tree)