亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于JQuery的asp.net樹實(shí)現(xiàn)代碼

 更新時(shí)間:2010年11月30日 17:11:57   投稿:mdxy-dxy  
在網(wǎng)上找了tree,想直接拿來用,誰知道竟然沒有找到基于asp.net的tree,索性自己便把jquery的tree拿來研究了下,然后結(jié)合者asp.net,做了一個(gè)遞歸樹.

本tree的數(shù)據(jù)從sql的表中提取而來,sql表的結(jié)構(gòu)如下:

上面的表中  parentmodeuleID是代表父ID的標(biāo)志,如果當(dāng)前節(jié)點(diǎn)為根節(jié)點(diǎn),則規(guī)定為0. 

然后就是如何將上面的單表來組成樹狀結(jié)構(gòu).這時(shí)我們可以利用IList來加載數(shù)據(jù)庫models來實(shí)現(xiàn),具體Tree類如下:

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


namespace RolePermission1
{
 public class Tree
 {
  public int ModuleID { get; set; }

  public int ParentID { get; set; }

  public string ModulePath { get; set; }

  public string ModuleName { get; set; }

  
 }
}

然后就是在公共處理頁面,將數(shù)據(jù)庫的數(shù)據(jù)進(jìn)行組織,形成符合jquery tree的json格式,具體代碼如下:

[WebMethod]
  public static string GetJson()
  {
   string json = "[";
   IList<Tree> t = DB.returnParentTree();
   foreach (Tree model in t)
   {
    if (model != t[t.Count - 1])
    {
     json += GetJsonByModel(model) + ",";
    }
    else
    {
     json += GetJsonByModel(model);
    }
   }
   json += "]";
   json=json.Replace("'","\"");
   return json;
  }

  public static string GetJsonByModel(Tree t)
  {
   string json = "";
   bool flag = DB.isHaveChild(t.ModuleID);

   json = "{"
      + "'id':'" + t.ModuleID + "',"
      + "'text':'" + t.ModuleName + "',"
      + "'value':'" + t.ModuleID + "',"
      + "'showcheck':true,"
      + "'checkstate':'0',"
      + "'hasChildren':" + flag.ToString().ToLower() + ","
      + "'isexpand':false,"
      + "'ChildNodes':"; /*奶奶的,這個(gè)地方一定要注意是ChildNodes 而不是childNodes 害得我無語了*/
   if (!flag)
   {
    json += "null,";
    json += "'complete':false}";
   }
   else
   {
    json += "[";
    IList<Tree> list = DB.getChild(t.ModuleID);
    foreach (Tree tree in list)
    {
     if (tree != list[list.Count - 1])
     {
      json += GetJsonByModel(tree) + ",";
     }
     else
     {
      json += GetJsonByModel(tree);
     }
    }
    json += "],'complete':true}";
   }
   return json;
  }

上面就是利用遞歸的方式來將數(shù)據(jù)庫的數(shù)據(jù)組合成合適的json數(shù)據(jù),利用到的數(shù)據(jù)庫操作類代碼如下:

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

namespace RolePermission1
{
 public class DB
 {

  public static readonly string connStr=System.Configuration.ConfigurationManager.AppSettings["connStr"];

  public static SqlConnection GetConn()
  {
   SqlConnection conn = new SqlConnection(connStr);
   conn.Open();
   return conn;
  }

  public static DataTable GetDT(string sql)
  {
   DataTable dt = new DataTable();
   using (SqlConnection conn = DB.GetConn())
   {
    SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
    sda.Fill(dt);
   }
   return dt;
  }

  public static IList<Tree> returnParentTree()
  {
   IList<Tree> t = new List<Tree>();
   string sql = "select * from Models where [ParentModuleID]=0 order by OrderBy asc";
   DataTable dt = GetDT(sql);
   foreach (DataRow dr in dt.Rows)
   {
    Tree tParent = new Tree();
    tParent.ModuleID = Int32.Parse(dr["ID"].ToString());
    tParent.ModuleName = dr["ModuleName"].ToString();
    tParent.ModulePath = dr["MenuPath"].ToString();
    tParent.ParentID = Int32.Parse(dr["ParentModuleID"].ToString());
    t.Add(tParent);
   }
   return t;
  }

  public static bool isHaveChild(int id)
  {
   bool flag=false;
   string sql = "select ID from Models where ParentModuleID="+id+"";
   DataTable dt = GetDT(sql);
   if (dt.Rows.Count > 0)
   {
    flag = true;
   }
   return flag;
   
  }
  public static IList<Tree> getChild(int id)
  {
   IList<Tree> t = new List<Tree>();
   string sql = "select * from Models where ParentModuleID=" + id + "";
   DataTable dt = GetDT(sql);
   foreach (DataRow dr in dt.Rows)
   {
    Tree tParent = new Tree();
    tParent.ModuleID = Int32.Parse(dr["ID"].ToString());
    tParent.ModuleName = dr["ModuleName"].ToString();
    tParent.ModulePath = dr["MenuPath"].ToString();
    tParent.ParentID = Int32.Parse(dr["ParentModuleID"].ToString());
    t.Add(tParent);
   }
   return t;
  }

 }
}

好了,當(dāng)json數(shù)據(jù)處理好以后,就可以將json打到前臺(tái),交給jquery來處理了,

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RolePermission1._Default" %>
<!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>
 <script src="jquery.treeview/lib/jquery.js" type="text/javascript"></script>
 <link href="jquery.treeview/tree.css" rel="stylesheet" type="text/css" />
 <script src="jquery.treeview/common.js" type="text/javascript"></script>
 <script src="jquery.treeview/jquery.tree.js" type="text/javascript"></script>
</head>
<body>
 <form id="form1">
  <button id="showchecked" runat="server">Get Selected Nodes</button>
 <div id="showTree" class="filetree">
 </div>
 </form>
</body>
 <script type="text/javascript">
  $(document).ready(function() {
  $.ajax({
   type: "post",
   contentType: "application/json;charset=utf-8",
   dataType: "json",
   url: "WebForm1.aspx/GetJson",
   success: function(data) {
    var o = { showcheck: true };
    o.data = eval(data.d);
    $("#showTree").treeview(o);
   }
   });
 });
 $("#showchecked").click(function(e) {
  var s = $("#showTree").getTSVs();
  if (s != null)
   alert(s.join(","));
  else
   alert("NULL");
 });
 </script>
</html>

好了,來看看加載結(jié)果吧:


制作過程中需要注意的是:首先,遞歸必須正確;其次注意js大小寫('ChildNodes'被我寫成了'childNodes',花費(fèi)了我一天時(shí)間才調(diào)整過來 暈哦) 再者就是可以直接調(diào)用公共頁面的方法,只要在方法前面加上[webmethod]標(biāo)記即可.

相關(guān)文章

  • 淺談jQuery 中的事件冒泡和阻止默認(rèn)行為

    淺談jQuery 中的事件冒泡和阻止默認(rèn)行為

    下面小編就為大家?guī)硪黄獪\談jQuery 中的事件冒泡和阻止默認(rèn)行為。小編舉得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • Jquery中的offset()和position()深入剖析

    Jquery中的offset()和position()深入剖析

    jquery 中有兩個(gè)獲取元素位置的方法offset()和position(),這兩個(gè)方法之間有什么異同?使用的時(shí)候應(yīng)該注意哪些問題?什么時(shí)候使用offset(),什么時(shí)候又使用position()呢?
    2009-05-05
  • 在Web項(xiàng)目中引入Jquery插件報(bào)錯(cuò)的完美解決方案(圖解)

    在Web項(xiàng)目中引入Jquery插件報(bào)錯(cuò)的完美解決方案(圖解)

    這篇文章主要介紹了在Web項(xiàng)目中引入Jquery插件報(bào)錯(cuò)的完美解決方案的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • jquery zTree異步加載簡(jiǎn)單實(shí)例講解

    jquery zTree異步加載簡(jiǎn)單實(shí)例講解

    這篇文章主要為大家詳細(xì)介紹了jquery zTree異步加載簡(jiǎn)單實(shí)例,ztree采用了延遲加載技術(shù),上萬節(jié)點(diǎn)輕松加載,即使在 IE6 下也能基本做到秒殺,感興趣的小伙伴們可以參考一下
    2016-02-02
  • jQuery 重復(fù)加載錯(cuò)誤以及修復(fù)方法

    jQuery 重復(fù)加載錯(cuò)誤以及修復(fù)方法

    本文主要記錄了在項(xiàng)目中遇到j(luò)Query重復(fù)加載導(dǎo)致依賴jQuery的js全部失效然后一步步分析,得出最終解決方案的全部過程,主要是記錄下來,提醒自己以后不要再犯相同錯(cuò)誤。
    2014-12-12
  • jQuery 調(diào)用WebService 實(shí)例講解

    jQuery 調(diào)用WebService 實(shí)例講解

    以前都是Web程序調(diào)用WebService,.net把WebService封裝的太簡(jiǎn)單了,所以之前都是很膚淺的用,都沒有想過更深層的東西,并且只是停留在表面的添加引用和調(diào)用。
    2016-06-06
  • jQuery ztree實(shí)現(xiàn)動(dòng)態(tài)樹形多選菜單

    jQuery ztree實(shí)現(xiàn)動(dòng)態(tài)樹形多選菜單

    這篇文章主要介紹了jQuery ztree實(shí)現(xiàn)動(dòng)態(tài)樹形多選菜單,ztree動(dòng)態(tài)樹形菜單,初始化加載和延遲加載,感興趣的小伙伴們可以參考一下
    2016-08-08
  • jquery彈出層類代碼分享

    jquery彈出層類代碼分享

    jquery彈層類代碼分享,大家參考使用吧
    2013-12-12
  • 24款非常有用的 jQuery 插件分享

    24款非常有用的 jQuery 插件分享

    jQuery在現(xiàn)在的Web開發(fā)項(xiàng)目中扮演著重要角色,jQuery讓網(wǎng)站有更好的可用性和用戶體驗(yàn),讓訪問者對(duì)網(wǎng)站留下非常好的印象。
    2011-04-04
  • jQuery制作仿騰訊web qq用戶體驗(yàn)桌面

    jQuery制作仿騰訊web qq用戶體驗(yàn)桌面

    ,酷炫個(gè)性的desktop桌面特效展示支持各大主流瀏覽器IE6以上,感興趣的朋友可以體驗(yàn)下哈
    2013-08-08

最新評(píng)論