首页 > 代码库 > 简单的加载 ztree
简单的加载 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=http://www.mamicode.com/"script/jquery-1.4.2.js"></script> <script src=http://www.mamicode.com/"script/jquery.ztree-2.6.js"></script> <link href=http://www.mamicode.com/"style/zTreeStyle/zTreeStyle.css" rel="stylesheet" /> <link href=http://www.mamicode.com/"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 = http://www.mamicode.com/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(); } } } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。