当前位置:编程学习 > C#/ASP.NET >>

asp.net DataGridTree 下拉树 实现方法

asp教程.net datagridtree 下拉树 实现方法

下拉树实现原理 输出json到客户端 客户端实现动态加载 中间不会和服务端交互 数据量支持上 经测试 几千 还是很快的 本下拉树控件是用c#+js树实现


--------------------------------------------------------------------------------

 

2.c# 计算器 计算字符串数学表达式源码  
计算数学表达式原理 采用c#实现 很实用
//a.建立两个栈:第一个位操作数栈,第二个操作符符栈!(将栈定义为string类型)
//b.对数字来说是无条件压入数字栈中.
//c.而对符号来说,只有当前栈顶元素的优先值小于扫到的符号时(比如”+”小于”*”),此符号才压入栈;否则大于等于的情况是将当前栈顶元素弹出栈,与当前数字栈的前两个数字组成式子进行计算.计算结果当作数字压入数字栈作为栈顶元素(要舍弃已经弹出的两个数字),而那个扫描到的符号则将代替那个弹出的符号作为栈顶元素)。
//d.最后说一下括号,原则是扫描到左括号时无条件压入符号栈,而扫到右括号时,则弹出离栈顶最近的一个左括号以上的全部符号与数字栈的数字做运算


--------------------------------------------------------------------------------

 

3.asp.net教程 datagridtree表格树控件 
继承asp.net的datagrid控件实现的表格树控件
/*表格树控件说明
* 此控件继承datagrid 新增属性说明:
* 1.treeparentcode:顶级根节点parentcode
* 2.treedisplaydeep:展现表格树深度默认为1
* 3.sumcolumns:自动汇总到根节点的字段集合 针对 decimal类型
* 4.新增树状列模板templatetreecolumn 此模板继承了templatecolumn 重写了方法initializecell
* 客户端新增特性配置说明
* 1.固定列 配置 itemstyle-css教程class='tdlockedclass'
* 2.固定表头 配置 headerstyle-cssclass='trlockedclass'
* 3.文本框 input 或 <asp:textbox 配置事件onchange='sumparent(this);' 数字改变相应所有父节点也随着改变 针对数字型 其他不支持
* 不过可以自定义js
* 报表说明:
* 1.datagridtree.enableviewstate=false;提高加载速度
* 2.动态定义列 实现 boundcolumn column = new boundcolumn();
column.headertext = "动态列";
column.datafield = "unitname";
datagridnew.columns.add(column);
* 也可以自定义默认模板 动态加载模板 定义模板例子templatetreecolumn,不用继承templatecolumn,实现接口 itemplate initializecell 方法就可以了
* 不足之处:1.对于复杂多行表头 不知 如何实现
* 2.表头和列固定 数据量大时 会影响反映速度 一千左右的数据量 还时没问题的 数据量在大的话 课考虑采用ajax动态加载 目前此功能还没实现


实例代码

private void maketree(datatable dtnodesets, string strparentcolumn, string strrootvalue, string strindexcolumn, string strtextcolumn, dropdownlist drpbind, int i)
  {
  //每向下一层,多一个缩入单位  
  i++;

  dataview dvnodesets = new dataview(dtnodesets);
  dvnodesets.rowfilter = strparentcolumn + "=" + strrootvalue;

  string strpading = ""; //缩入字符  

  //通过i来控制缩入字符的长度,我这里设定的是一个全角的空格  
  for (int j = 0; j < i; j++)
  strpading += " ";//如果要增加缩入的长度,改成两个全角的空格就可以了  

  foreach (datarowview drv in dvnodesets)
  {
  treenode tnnode = new treenode();
  listitem li = new listitem(strpading + "├" + drv[strtextcolumn].tostring(), drv[strindexcolumn].tostring());
  drpbind.items.add(li);
  maketree(dtnodesets, strparentcolumn, drv[strindexcolumn].tostring(), strindexcolumn, strtextcolumn, drpbind, i);
  }

  //递归结束,要回到上一层,所以缩入量减少一个单位  
  i--;
  }

  /// <summary>  
  /// sql语句查询,再绑定到droplist里面  
  /// </summary>  
  private void createtree()
  {
  //查询zonelist  
  string sql = "select * from master_department where parent_department='003'";
  dataset ds = db.getds();
  datatable dt = ds.tables[0];

  maketree(dt, "parent_department", "003", "department_code", "department_name", dropdownlist1, -1);
  }


网上找的另一个比较好的实例

using system;
using system.collections.generic;
using system.text;
using system.web.ui.webcontrols;
namespace inte易做图ce.common
{
    public inte易做图ce idropdowntree : idisposable
    {
        /**//// <summary>
        /// 返回dictionary里分别对应id,文本,如果没有子节点返回null
        /// </summary>
        /// <param name="parentid">父节点id</param>
        /// <returns></returns>
        dictionary<string, string> getchildcategory(string parentid);
        /**//// <summary>
        /// 代码里写return new inte易做图ce.common.dropdowntree(this);
        /// </summary>
        dropdowntree dropdowntree
        {
            get;
        }
    }
    public sealed class dropdowntree
    {
        idropdowntree _dropdowntree;
        public dropdowntree(idropdowntree dropdowntree)
        {
            _dropdowntree = dropdowntree;
        }
        /**//// <summary>
        /// 用于树的前缀
        /// </summary>
        /// <param name="islast">是否是同级节点中的最后一个</param>
        /// <param name="haschild">本节点是否拥有子节点</param>
        /// <param name="parentstring">父节点前缀符号</param>
        /// <returns>本节点的前缀</returns>
        private string getprefix(bool islast, bool haschild, string parentstring)
        {
            string result = string.empty;
            if (!string.isnullorempty(parentstring))
            {
                parentstring = parentstring.remove(parentstring.length - 1).replace("├", "│").replace("└", " ");
                result += parentstring;
            }
            if (islast)
            {
                result += "└";
            }
            else
            {
                result += "├";
            }
            if (haschild)
            {
                result += "┬";
            }
            else
            {
                result += "─";
            }
            return result;
        }
        绑定下拉菜单#region 绑定下拉菜单
        /**//// <summary>
        /// 绑定连动级的下拉菜单
        /// </summary>
        /// <param name="ddlgoodstype">传进一个被绑定的dropdownlist</param>
        /// <param name="removeid">被排除绑定的节点id</param>
        /// <param name="autodispose">是否自动释放</param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid,string parentid, bool autodispose)
        {
            if (ddlgoodstype != null)
            {
                listitem listitem = null;
                string currentid = parentid;//根节点/父id
                string currentsign = string.empty;//当前节点符号;
                string parrentsign = string.empty; //父节点符号;
                bool haschild = true;//是否有子
                queue<string> parentkeylist = new queue<string>();//存 有子节点的 节点id
                queue<string> parentsignlist = new queue<string>();//对应节点id的前缀符号
                int itemindexof = 0;//父节点所在的位置 
                while (haschild)
                {
                    int lastonecount = 1;//用于计算在同级别中是否最后一个
                    dictionary<string, string> childlist = _dropdowntree.getchildcategory(currentid);// 得到子节点列表
                    if (childlist != null && childlist.count > 0)
                    {
                        if (!string.isnullorempty(removeid) && childlist.containskey(removeid))
                        {
                            childlist.remove(removeid);
                        }
                        foreach (keyvaluepair<string, string> entry in childlist)
                        {
                            if (_dropdowntree.getchildcategory(entry.key) != null)//存在子
                            {
                                currentsign = getprefix(lastonecount == childlist.count, true, parrentsign);
                                listitem = new listitem(currentsign + entry.value, entry.key);
                                parentkeylist.enqueue(entry.key);//当前的节点id
                                parentsignlist.enqueue(currentsign);//当前的节点符号
                            }
                            else//不存在子
                            {
                                currentsign = getprefix(lastonecount == childlist.count, false, parrentsign);
                                listitem = new listitem(currentsign + entry.value, entry.key);
                            }
                            if (ddlgoodstype.items.count != 0)
                            {
                                itemindexof = string.isnullorempty(currentid) ? itemindexof + 1 : ddlgoodstype.items.indexof(ddlgoodstype.items.findbyvalue(currentid)) + lastonecount;
                            }
                            ddlgoodstype.items.insert(itemindexof, listitem);//添加子节点
                            lastonecount++;
                        }
                        if (parentkeylist.count > 0)//存在子节点时
                        {
                            currentid = parentkeylist.dequeue();
                            parrentsign = parentsignlist.dequeue();
                        }
                        else
                        {
                            haschild = false;
                        }
                    }
                    else
                    {
                        break;
                    }

                }
                if (autodispose)
                {
                    _dropdowntree.dispose();
                }
            }
        }
        /**//// <summary>
        /// 绑定连动级的下拉菜单
        /// </summary>
        /// <param name="ddlgoodstype">传进一个被绑定的dropdownlist</param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype)
        {
            bindtodropdownlist(ddlgoodstype, string.empty,null, true);
        }
        /**//// <summary>
        /// 绑定连动级的下拉菜单
        /// </summary>
        /// <param name="ddlgoodstype">传进一个被绑定的dropdownlist</param>
        /// <param name="removeid">被排除的id</param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid)
        {
            bindtodropdownlist(ddlgoodstype, removeid,null, true);
        }
        /**//// <summary>
        /// 绑定连动级的下拉菜单
        /// </summary>
        /// <param name="ddlgoodstype">传进一个被绑定的dropdownlist</param>
        /// <param name="removeid">被排除的id,若没有,传null</param>
        /// <param name="parentid">起始父id</param>
        public void bindtodropdownlist(dropdownlist ddlgoodstype, string removeid,string parentid)
        {
            bindtodropdownlist(ddlgoodstype, removeid,parentid, true);
        }
        #endregion
    }
}


调用方法很简单: 1.继承自idropdowntree接口 2.实现3个接口方法实现接口代码示例[dispose方法自己实现],最主要的是自己实现获得子级的方法

idropdowntree 成员#region idropdowntree 成员
        public dictionary<string, string> getchildcategory(string parentid)
        {
            string where = "parentid='" + parentid + "'";
            if (string.isnullorempty(parentid))
            {
                where = "parentid is null or parentid='" + guid.empty + "'";
            }
            list<goodscategorybean> _goodscategorylist = selectlist(0, where, string.empty, false);
            if (_goodscategorylist != null && _goodscategorylist.count > 0)
            {
                dictionary<string, string> categorylist = new dictionary<string, string>();
                for (int i = 0; i < _goodscategorylist.count; i++)
                {
                    categorylist.add(_goodscategorylist[i].id.tostring(), _goodscategorylist[i].gategoryname);
                }
                return categorylist;
            }//51aspx.com
            return null;
        }
        public inte易做图ce.common.dropdowntree dropdowntree
        {
            get { return new inte易做图ce.common.dropdowntree(this); }
        }
        #endregion

页面调用代码: 类名.dropdowntree.bindtodropdownlist(下拉控件id);

补充:asp.net教程,.Net开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,