>>51aspx首页 | >>Winform源码 | >>.Net源码大搜捕 | >>问题/帮助

Asp.net源码交流论坛

登录 注册
  • 标签
  • 会员
  • 搜索
  • 帮助

Asp.net源码交流论坛 » Asp.net交流讨论区 » Asp.net技术文章 » 线行无限级下拉树列表

帖子标题
业务申请区
  • 实名认证申请
  • 免费.Net主机申请
  • 积分奖励申请
  • 有偿服务申请
Asp.net交流讨论区
  • Asp.net技术问答
  • .Net项目、服务交易区
  • .Net源码问答区
  • Asp.net技术文章
  • 常用工具下载
Asp.net资源发布区
  • [源码发布区]
  • [视频发布区]
  • [商业代码区]
  • [其他.net相关资源]
Asp.net专题讨论区
  • Ajax/Atlas无刷新技术
  • 面向对象开发
  • ADO.net讨论区
  • 控件专题讨论
源码环境搭建和部署视频! 源码使用常见问题 51Aspx自有服务产品 开业特惠 快速通道:[源码发布] | [实名认证]
1/1页1 跳转到页查看:2483
发新话题 回复该主题
键盘左右键可以进行前后翻页操作
帮助

线行无限级下拉树列表

离线 51aspx
头像

51aspx

  • [超级管理员]
  • [2794]
  • 2068
  • 2007-05-17
源码贡献奖实名用户
51aspx 2008-04-17 14:38 | 只看楼主 树型| 收藏| 小 中 大 1 #

线行无限级下拉树列表

[实名用户所发信息,推荐关注实名贴,值得关注]


好多年没写文章了
这里就分享点自己原创的一点破代码,效果如图下:

本人的提供的代码如下:

[复制到剪贴板]
CODE:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
namespace Interface.Common
{
    public interface IDropDownTree : IDisposable
    {
        /**//// <summary>
        /// 返回Dictionary里分别对应ID,文本,如果没有子节点返回null
        /// </summary>
        /// <param name="parentID">父节点ID</param>
        /// <returns></returns>
        Dictionary<string, string> GetChildCategory(string parentID);
        /**//// <summary>
        /// 代码里写return new Interface.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方法自己实现],最主要的是自己实现获得子级的方法

[复制到剪贴板]
CODE:
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 Interface.Common.DropDownTree DropDownTree
        {
            get { return new Interface.Common.DropDownTree(this); }
        }
        #endregion


页面调用代码: 类名.DropDownTree.BindToDropDownList(下拉控件ID);

原文作者:cyq1162
技术问题请直接发布到论坛,客户服务QQ:1120930903,合作及咨询QQ:793095132
常见问题:点这里,请仔细查看!
基础配置视频: 点这里
问题搜索:请点击!
积分奖励: 关于论坛实名版块的建议...
 

TOP

 

发送短消息

查看公共资料

查找该会员全部帖子

  • 2
  • 27
  • 591 分
  • 364.6 元
  • 北京
  • 离线
  • QQ: 793095132 793095132
离线 zhcsmx22
头像

  • [实习生]
  • [16]
  • 16
  • 2007-12-08
zhcsmx22 2008-11-09 16:38 树型| 收藏| 小 中 大 2 #

回复:线行无限级下拉树列表



学习。。 。。。
 

TOP

 

发送短消息

查看公共资料

查找该会员全部帖子

  • 2136
  • 0
  • 0 分
  • 1.9 元
  • 离线
离线 375267603
头像

  • [实习生]
  • [25]
  • 9
  • 2009-08-28
375267603 2009-08-28 15:40 树型| 收藏| 小 中 大 3 #

不知道怎么调用....



首先感谢你的代码,很详细,但由于我是新手,希望教下我
 

TOP

 

发送短消息

查看公共资料

查找该会员全部帖子

  • 66134
  • 0
  • 16 分
  • -34 元
  • 离线
离线 sanler
头像

三乐

  • [工程师]
  • 1980-12-17
  • [200]
  • 84
  • 2009-09-15
实名用户
sanler 2009-10-03 22:36 树型| 收藏| 小 中 大 4 #

[实名用户所发信息,推荐关注实名贴,值得关注]


暂时收藏,以后再研究
 

TOP

 

发送短消息

查看公共资料

查找该会员全部帖子

  • 67665
  • 0
  • 116 分
  • -28 元
  • 广东,湛江
  • 离线
  • QQ: 9424106 9424106
<<上一主题|下一主题>>
1/1页1 跳转到页
发表新主题 回复该主题

相关主题

漂亮的NavMenu导航控件使用和配置
分享一个一个简单智能js菜单(可在线演示)
在TreeView中操作节点CheckBox需要注意的
写了一个 树形菜单 看起来还不错!
  • 发新主题

Asp.net源码下载专业站  - 源码推荐 - 最新源码  Sitemap

bbs.51Aspx.com - 简洁版本 - TOP - 界面风格

  • Default

Discuz!NT

Powered by Discuz!NT 2.6.1 © 2000-2010 51Aspx.com.

Processed in 0.21875 second(s) , 5 queries. 京ICP备06046876号

  • 我的资料
  • 我的主题
  • 我的回复
  • 我的精华
  • 我的附件
  • 我的收藏
  • 基本状况
  • 流量统计
  • 客户软件
  • 发帖量记录
  • 版块排行
  • 主题排行
  • 发帖排行
  • 积分排行
  • 在线时间
帖子标题
作  者