基于JQuery的asp.net comboTree

本次封装采用的是JQuery 中的comboTree控件.在我接触的项目中,或多或少都用到了这个控件,所以就直接拿来封装下吧.具体的生成界面如下:

可以实现多选,并且可以实现获取多选的值.这样一来,在项目中,就可以不用使用大量的listbox或者是checkboxlist来进行绑定了.

具体的公共页面代码(comboTree.ashx)如下:

 
  
<% @ WebHandler Language = " C# " Class = " ComboTree " %>

using System;
using System.Web;
using System.Collections.Generic;

public class ComboTree : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType
= " text/plain " ;
context.Response.Write(GetJson());
}


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 + " ', "
+ " 'iconCls':'ok', "
+ " 'children': " ;
if ( ! flag)
{
json
+= " null} " ;
}
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
+= " ]} " ;
}
return json;
}


public bool IsReusable {
get {
return false ;
}
}

}

Tree类的代码如下:

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

/// <summary>
/// Tree 的摘要说明
/// </summary>
public class Tree
{
public int ModuleID { get ; set ; }

public int ParentID { get ; set ; }

public string ModulePath { get ; set ; }

public string ModuleName { get ; set ; }

}

数据操作类代码如下:

  

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

/// <summary>
/// DB 的摘要说明
/// </summary>
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;
}
}

都是比较简单的,所以就没有做过的的注释.  需要注意的是,在comboTree.ashx页面中,是用到了递归来生成JSON数据的,所以在利用递归生成的时候,一定要注意数据的正确性,否则会出现错误的.

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值