Implementation Code of the asp.net tree based on JQuery _ jquery

Source: Internet
Author: User
I found a tree on the Internet and wanted to use it directly. Who knows that I didn't find a tree based on asp.net? I simply took the jquery tree and studied it, and then combined it with asp.net, build a recursive tree. the data of this tree is extracted from the SQL table. The structure of the SQL table is as follows:

In the preceding table, parentmodeuleID indicates the parent ID. If the current node is the root node, it is set to 0.

Then we can use IList to load the database models. The specific Tree class is as follows:

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; }   }}

Then, on the public processing page, organize the database data to form a json format that complies with the jquery tree. The Code is as follows:

[WebMethod] public static string GetJson () {string json = "["; IList
 
  
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," + "'ch Eckstate ': '0', "+" 'haschildren': "+ flag. toString (). toLower () + "," + "'isexpand': false," + "'childnodes ':";/* grandma's, please note that ChildNodes is not childNodes, which makes me speechless */if (! Flag) {json + = "null,"; json + = "'complete': false}" ;}else {json + = "["; IList
  
   
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 ;}
  
 

The preceding Code uses recursive methods to combine database data into appropriate json data. The database operation code is as follows:

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
 
   returnParentTree()  {   IList
  
    t = new List
   
    ();   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
    
      getChild(int id)  {   IList
     
       t = new List
      
       (); 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; } }}
      
     
    
   
  
 

Now, after processing the json data, you can call json to the front-end and submit it to jquery for processing,

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "Default. aspx. cs" Inherits = "RolePermission1. _ Default" %>  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.