双树 节点 C# net Ztree linqtosql 源码

架构设计 模块架构

 结果 操作左边的树异步刷新右边的树 双树 节点 C# net Ztree linqtosql 源码_第1张图片


从中需要用到

 JS 

ajax 

ztree  

js调用C#后台方法 

递归 

JS回调函数

linqtosql

C#基础知识


注意事项  :文章的ztree使用的标准对象模式 

使用ztree时 有两种方式   都对数据库的设计有很大的要求 

主要要有ID 和 ParentID

使用简单对象模式时 需要注意还要加一个ISparent 字段 判断是否为节点

一下是使用标准对象模式的源码 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MS.DAL.DBML;
using MS.DAL.LINQ_DAL.Common;

namespace MS.Web.MS_Styseting.MS_Organize
{
    public class Company_childrenweb 
    {
        private List _children;

        public List children
        {
            get { return _children; }
            set { _children = value; }
        }

        private string _cId;

        public string cId
        {
            get { return _cId; }
            set { _cId = value; }
        }
        private string _cName;

        public string cName
        {
            get { return _cName; }
            set { _cName = value; }
        }

        private string _cParentId;

        public string cParentId
        {
            get { return _cParentId; }
            set { _cParentId = value; }
        }
        private string _cUrl;
        public string cUrl
        {
            get { return _cUrl; }
            set { _cUrl = value; }
        }

        private string _cParentrightID;
        public string cParentrightID
        {
            get { return _cParentrightID; }
            set { _cParentrightID = value; }
        }
    }
}




后台

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using MainloserTree.DAL;

namespace MainloserTree.Sys
{
    public partial class MailLoserZtreeView : System.Web.UI.Page
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["Maillosertree"].ToString();//连接数据库的


        public string Sys_Organizeid = "97fb8de4-56e7-457b-856d-388ba91ea5e0";
        public int Cid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        //XML里面的一个方法,在jquery用ajax方法调用;  ok01
        ///   
        /// 获取所有的一级节点  
        ///   
        /// 所有的节点信息  
        [WebMethod]
        public static List FindAll()
        {
            //实例化类  
            MailLoserZtreeView mS_OrganZtreeJ = new MailLoserZtreeView();
            string sqlText = "select * from Sys_Organize where ParentID=0";
            //定义实体类列表  
            List list = new List();
            //将所有的一级节点放到list中  
            list = mS_OrganZtreeJ.GetParentID(sqlText);
            // mS_OrganZtreeJ.GetChilds(ref list);  
            return list;
        }



        //XML里面的一个方法,在jquery用ajax方法调用;  OK0201
        ///   
        /// 获取所有的一级节点   Q群196997099
        ///   
        /// 所有的节点信息  
        /// 
        [WebMethod]
        public static List FindAllCompanyK(string Sys_Organizeid)
        {
            //实例化类 string Sys_Organizeid
            if (Sys_Organizeid == null)
            {
                Sys_Organizeid = "97fb8de4-56e7-457b-856d-388ba91ea5e0";
            }
            MailLoserZtreeView mS_OrganZtreeJ = new MailLoserZtreeView();
            //表连接   Q群196997099
           string sqlText =
                String.Format(@"select * from dbo.Company c join Sys_Organize s on c.Organizeid=s.ID where s.ID='{0}'",
                              Sys_Organizeid);
            //定义实体类列表  
            List listCompany = new List();
            //将所有的一级节点放到list中  
            listCompany = mS_OrganZtreeJ.GetCompanyParentID(sqlText); // OK0202
            mS_OrganZtreeJ.GetChildslistCompany(ref listCompany); // OK0204  
            return listCompany;
        }

        ///   
        ///获所有的一级节点  
        ///   
        ///   
        ///   
        public List GetParentID(string sql)
        {
            List user = new List();
            SqlDataReader reader = null;
            reader = ExecuteReader(sql);
            while (reader.Read())
            {
                user.Add(ObjToListC(reader));
            }
            return user;
        }


        ///   
        ///获所有的一级节点  ok0202
        ///   
        ///   
        ///   
        private List GetCompanyParentID(string sql)
        {
            List user = new List();
            SqlDataReader reader = null;
            reader = ExecuteReader(sql);
            while (reader.Read())
            {
                user.Add(ObjToListKCompany(reader)); //ok0203
            }
            return user;
        }

        ///   
        /// 将获取的所有的子节点放到children中  
        ///   
        /// 按引用传值,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中  
        public void GetChilds(ref List list)
        {

            foreach (Sys_Organize modelChilds in list)
            {

                //通过上级ID获取子级,然后添加到lstModel中  
                List lstModel = GetChildsID(modelChilds.ID);
                if (lstModel.Count > 0)
                {
                    modelChilds.ID = lstModel[0].ID;
                    modelChilds.ParentID = lstModel[0].ParentID;
                    modelChilds.Remark = lstModel[0].Remark;
                    modelChilds.OrganizeCode = lstModel[0].OrganizeCode;
                    modelChilds.OrganizeName = lstModel[0].OrganizeName;
                    //  modelChilds.children = lstModel;  
                    //  model.;osmf = lstModel;
                    //采用递归的形式  
                    GetChilds(ref lstModel);
                }
            }
        }

        ///   
        /// 将获取的所有的子节点放到children中  ok0204
        ///   
        /// 按引用传值,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中  
        public void GetChildslistCompany(ref List list)
        {
            
            foreach (Company_childrenweb modelChilds in list)
            {
                //通过上级ID获取子级,然后添加到lstModel中  Q群196997099
                List lstModelCompany = GetChildsIDCompany(modelChilds.cId);
                if (lstModelCompany.Count > 0)
                {
                    modelChilds.children = lstModelCompany;  //这个就是树的关键难点 这个决定了与夫节点的关系 加一个list到对象里去
                    //采用递归的形式   
                    GetChildslistCompany(ref lstModelCompany);
                }
            }
        }

        ///   
        /// 根据父节点的id获取子节点  
        ///   
        /// 父节点id  
        /// 所有的子节点id  
        public List GetChildsIDCompany(string dParentId)
        {
            //定义一个列  
            List result = new List();
            SqlDataReader sqlReader = null;
            //  var  ObjToListCCompanybbb;
            string strSql = String.Format(@"select * from Company where ParentrightID='{0}'", dParentId);
            sqlReader = ExecuteReader(strSql);
            while (sqlReader.Read())
            {
                result.Add(ObjToListKCompany(sqlReader));
            }
            sqlReader.Close();
            return result;
        }

        ///   
        /// 根据父节点的id获取子节点  
        ///   
        /// 父节点id  
        /// 所有的子节点id  
        public List GetChildsID(string dParentId)
        {
            //定义一个列  
            List result = new List();
            SqlDataReader sqlReader = null;
            string strSql = String.Format(@"select * from Sys_Organize where ID='{0}'", dParentId);
            sqlReader = ExecuteReader(strSql);
            while (sqlReader.Read())
            {
                result.Add(ObjToListC(sqlReader));
            }
            sqlReader.Close();
            return result;
        }

        ///   
        /// 解析查询结果  
        ///   
        ///   
        /// 父节点id,子节点id,子节点名字  
        private Sys_Organize ObjToListC(SqlDataReader reader)
        {
            Sys_Organize model = new Sys_Organize();
            if (reader != null)
            {
                //子节点id  
                if (reader["ParentID"] != null && reader["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(
                        reader["parentID"].ToString()
                        )
                        ;
                }
                //节点名字  
                if (reader["OrganizeName"] != null && reader["OrganizeName"].ToString() != "")
                {
                    model.OrganizeName = reader["OrganizeName"].ToString();
                }
                //父节点id  
                if (reader["ParentID"] != null && reader["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(
                        reader["ParentID"].ToString()
                        );
                }
                //地址  
                if (reader["Remark"] != null && reader["Remark"].ToString() != "")
                {
                    model.Remark = reader["Remark"].ToString();
                }

                //code 
                if (reader["OrganizeCode"] != null && reader["OrganizeCode"].ToString() != "")
                {
                    model.OrganizeCode = reader["OrganizeCode"].ToString();
                }

                //节点名字  
                if (reader["ID"] != null && reader["ID"].ToString() != "")
                {
                    model.ID = reader["ID"].ToString();
                }
                //节点名字  
                if (reader["Sort"] != null && reader["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(
                        reader["Sort"].ToString());
                }

            }
            return model;
        }



        ///   
        /// 解析查询结果  
        ///   
        ///   
        /// 父节点id,子节点id,子节点名字  
        private Company_childrenweb ObjToListKCompany(SqlDataReader reader)
        {
            Company_childrenweb model = new Company_childrenweb();
            if (reader != null)
            {
                //子节点id  
                if (reader["ID"] != null && reader["ID"].ToString() != "")
                {
                    model.cId = reader["ID"].ToString();
                    //  model.ID = int.Parse(reader["ID"].ToString());
                }
                //节点名字  
                if (reader["CompanyName"] != null && reader["CompanyName"].ToString() != "")
                {
                    model.cName = reader["CompanyName"].ToString();
                }
                //父节点id  
                if (reader["ParentrightID"] != null && reader["ParentrightID"].ToString() != "")
                {
                    model.cParentId = reader["ParentrightID"].ToString();
                }
                //id  
                if (reader["Organizeid"] != null && reader["Organizeid"].ToString() != "")
                {
                    model.cUrl = reader["Organizeid"].ToString();
                }

            }
            return model;
        }

        ///   
        /// 解析查询结果  
        ///   
        ///   
        /// 父节点id,子节点id,子节点名字  
        private static Company ObjToListCCompany(SqlDataReader reader)
        {
            Company model = new Company();
            if (reader != null)
            {
                //子节点id  
                if (reader["ID"] != null && reader["ID"].ToString() != "")
                {
                    model.ID = int.Parse(reader["ID"].ToString());
                }
                //节点名字  
                if (reader["CompanyName"] != null && reader["CompanyName"].ToString() != "")
                {
                    model.CompanyName = reader["CompanyName"].ToString();
                }
                //父节点id  
                if (reader["Levelid"] != null && reader["Levelid"].ToString() != "")
                {
                    model.Levelid = int.Parse(reader["Levelid"].ToString());
                }
                //id  
                if (reader["categoryid"] != null && reader["categoryid"].ToString() != "")
                {
                    model.categoryid = reader["categoryid"].ToString();
                }

                //code 
                if (reader["companytype"] != null && reader["companytype"].ToString() != "")
                {
                    model.companytype = reader["companytype"].ToString();
                }

                //节点名字  
                if (reader["Organizeid"] != null && reader["Organizeid"].ToString() != "")
                {
                    model.Organizeid = reader["Organizeid"].ToString();
                }
                //节点名字  
                if (reader["CompanyID"] != null && reader["CompanyID"].ToString() != "")
                {
                    model.CompanyID = reader["CompanyID"].ToString();
                }
                //节点名字  
                if (reader["ParentrightID"] != null && reader["ParentrightID"].ToString() != "")
                {
                    model.ParentrightID = int.Parse(reader["ParentrightID"].ToString());
                }
            }
            return model;
        }


        private static SqlDataReader ExecuteReader(string strSQL)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(strSQL, connection);
            try
            {
                connection.Open();
                SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return myReader;
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                throw e;
            }
        }
    }
}




前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MailLoserZtreeView.aspx.cs" Inherits="MainloserTree.Sys.MailLoserZtreeView" %>





    MS_OrganZtreeJ
    
     
    
    
    
   
  

  
  
    


你可能感兴趣的:(C#,C#,Tree,节点,双树,架构)