随笔 - 394  文章 - 0  评论 - 946  阅读 - 143万 

本次封装采用的是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数据的,所以在利用递归生成的时候,一定要注意数据的正确性,否则会出现错误的.

 

 

 

 

posted on   程序诗人  阅读(4538)  评论(5编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示