简单的加载 ztree

HTML页面:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="script/jquery-1.4.2.js"></script>
    <script src="script/jquery.ztree-2.6.js"></script>
    <link href="style/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <link href="style/demoStyle/demo.css" rel="stylesheet" />

    <script type="text/javascript">
        var zTree;
        var demoIframe;
        //setting 是 zTree 的全部设置参数集合,采用 JSON 结构,便于灵活配置
        var setting = {
            isSimpleData: true,
            treeNodeKey: "id",         //设置节点唯一标识属性名称
            treeNodeParentKey: "pId",  //设置节点的父节点唯一标识属性名称
            nameCol: "name",           //设置 zTree 显示节点名称的属性名称,此处默认为Name
            showLine: true,            //在树型中是否显示线条样式
            root: {                    //zTree数据节点的根,全部节点数据都处于 root.nodes 内
                isRoot: true,
                nodes: []
            },
            async: true,
            asyncUrl: "asyncdemo.aspx?&time=" + Math.random(),
            asyncParam: ["id", "pId", "name"],    //异步调用时传到后台的参数
            callback: {
                asyncSuccess: zTreeOnAsyncSuccess
            }
        };

        var treeNodes = [<%= NodesData%>];


        $(document).ready(function () {
            zTree = $("#treeid").zTree(setting, treeNodes);
        });

        function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <ul id="treeid" class="tree" style="width: 230px; height: 100%; overflow: auto;">
        </ul>
    </div>
    </form>
</body>
</html>


.cs

 

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

namespace WebzTree
{
    public partial class Idex : System.Web.UI.Page
    {
        public StringBuilder NodesData = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            List<string> list = new List<string>();
            //List<Model> mfType = loadList();
            string Strtest = loadList();
            //foreach (Model model in mfType)
            //{
            //    string node = string.Format("{{ \"id\":{0}, \"pId\":{1}, \"name\":\"{2}\",\"url\":\"http://www.baidu.com\",\"isParent\":true}}",
            //      model.P_id, model.parent_id, model.P_Title);
            //    list.Add(node);
            //}

            //string Strtest = string.Join(",", list.ToArray());
            NodesData.Append(Strtest);
            string teststr = NodesData.ToString();
        }




        //public List<Model> loadList()
        //{
        //    StringBuilder jsonString = new StringBuilder();
        //    List<Model> list = new List<Model>();
        //    Model model = new Model();
        //    string sql = "select P_id,parent_id,P_Title from tb_public_data_resource";
        //    DataTable dt = sqlHelper.GetListTable(sql);
        //    foreach (DataRow dr in dt.Rows)
        //    {
        //        model.P_id = Convert.ToInt32(dr["P_id"]);
        //        model.parent_id = Convert.ToInt32(dr["parent_id"]);
        //        model.P_Title = dr["P_Title"].ToString();
        //        list.Add(model);
        //    }
        //    return list;
        //}

        public string loadList()
        {
            StringBuilder jsonString = new StringBuilder();

            List<Model> list = new List<Model>();
            Model model = new Model();
            string sql = "select P_id,parent_id,P_Title from tb_public_data_resource";
            DataTable dt = sqlHelper.GetListTable(sql);
            foreach (DataRow dr in dt.Rows)
            {

                model.P_id = Convert.ToInt32(dr["P_id"]);
                model.parent_id = Convert.ToInt32(dr["parent_id"]);
                model.P_Title = dr["P_Title"].ToString();
                string node = string.Format("{{ \"id\":{0}, \"pId\":{1}, \"name\":\"{2}\",\"url\":\"http://www.baidu.com\",\"isParent\":true}}",
                                model.P_id, model.parent_id, model.P_Title);
                jsonString.Append(node +",");
            }

            list.Add(model);

            return jsonString.ToString();
        }
    }
}

 

 sqlHelper:

 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WebzTree
{
    public class sqlHelper
    {
        private static string ConnectionString = ConfigurationManager.ConnectionStrings["DBConnString"].ConnectionString;

        private static SqlConnection _sqlCon;
        /// <summary>
        /// 获取一个新的连接和打开连接
        /// </summary>
        public static SqlConnection conn
        {
            get
            {
                if (_sqlCon == null)
                {
                    _sqlCon = new SqlConnection();
                    _sqlCon.ConnectionString = ConnectionString;
                }
                if (_sqlCon.State == ConnectionState.Closed)
                {
                    _sqlCon.Open();
                }
                return _sqlCon;
            }
        }

        /// <summary>
        /// 执行查询时使用
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <returns>DataTable</returns>
        public static DataTable GetListTable(string sql)
        {
            try
            {
                using (SqlCommand comm = new SqlCommand(sql, conn))
                {
                    using (SqlDataAdapter adpater = new SqlDataAdapter(comm))
                    {
                        DataSet dataset = new DataSet();
                        adpater.Fill(dataset);

                        return dataset.Tables[0];
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Close();
                }
            }
        }


    }
}

 

 

转载于:https://www.cnblogs.com/HyMagic/p/3792214.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值