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

  • 相关阅读:
    AutoFac (控制反转IOC 与依赖注入DI)
    【干货】利用MVC5+EF6搭建博客系统(一)EF Code frist、实现泛型数据仓储以及业务逻辑
    Log4.Net 在Winfrom、MVC、ashx程序里的使用,ashx程序里使用异步
    触发器
    游标的使用
    sql数据库快照与恢复 规则绑定
    磁盘恢复
    修改sql数据库名称
    MVC学习笔记
    NuGet包和功能
  • 原文地址:https://www.cnblogs.com/scy251147/p/1896763.html
Copyright © 2020-2023  润新知