实现ASPxComboBox下拉框绑定树形数据

原创 2018年04月17日 11:35:50

前端:

<dxe:ASPxComboBox ID="cbxClassId" runat="server" Width="250px" ClientInstanceName="cbxClassId">
</dxe:ASPxComboBox>

后台实现方法:

EditorBinder.BindCustomerType(cbxClassId, true);//绑定下拉框数据

public static void BindCustomerType(ASPxComboBox cbx, bool select)
        {
            cbx.ValueField = "CLASS_ID";
            cbx.TextField = "CLASS_NAME";
            var entities = CustomerService.GetCustomerClasses();//数据集
            if (select) entities.Insert(0, new CM_BAS_CUSTOMER_CLASS() { CLASS_NAME = "请选择", CLASS_ID = "" });
            LoadDropDown<CM_BAS_CUSTOMER_CLASS>(cbx, "CLASS_ID", "CLASS_NAME", "CLASS_ID", "PARENT_CLASS_ID", "", entities, true);
            //cbx.DataSource = entities;
            //cbx.DataBind();
        }

/// <summary>
        /// 加载下拉框
        /// </summary>
        /// <typeparam name="Category">可根据泛型获取数据</typeparam>
        /// <param name="ctl">DropDown控件ID或控件</param>
        /// <param name="propId">主键属性名称</param>
        /// <param name="propText">显示的属性名称</param>
        /// <param name="propValue">绑定的属性值</param>
        /// <param name="propParent">父节点的属性不为空则是树形</param>
        /// <param name="propSort">排序属性</param>
        /// <param name="dataTag">数据源或条件</param>
        /// <param name="isIdEqValue">propId是否与propValue相等</param>
        public static void LoadDropDown<Category>(object ctl, string propId, string propText, string propValue, string propParent, string propSort, object dataTag, bool isIdEqValue) where Category : class
        {
            if (string.IsNullOrEmpty(propId) || string.IsNullOrEmpty(propText) || string.IsNullOrEmpty(propValue)) return;

            string strWhere = "";
            List<Category> list = null;
            if (dataTag is IEnumerable<Category>)
                list = ((IEnumerable<Category>)dataTag).ToList();
            else
                strWhere = Convert.ToString(dataTag);
            if (list == null)
            {
                var bs = DataRepository.Container.ResolveAll<IBaseService<Category>>();
                if (bs.Count() <= 0) throw new ArgumentException("no component!");
                list = bs[0].GetByWhere(strWhere).AsQueryable().OrderBy(propSort).ToList();
            }
            IList<TreeViewNode> resultList = CreateDropDown<Category>(propId, propText, propValue, propParent, propSort, list, isIdEqValue);
            ASPxComboBox ddl = null;
            if (ctl is Control)
                ddl = ctl as ASPxComboBox;
            else
                ddl = FindControl(HttpContext.Current.CurrentHandler as Page, ctl.ToString()) as ASPxComboBox;
            if (ddl == null) throw new ArgumentNullException("ComboBox is null!");

            for (int i = ddl.Items.Count - 1; i >= 0; i--)
            {
                if (ddl.Items[i].Value != null && ddl.Items[i].Value.ToString() != "")
                    ddl.Items.RemoveAt(i);
            }
            foreach (var tv in resultList)
            {
                ddl.Items.Add(tv.Text, tv.Value);
            }
            //ddl.DataSource = resultList;
            //ddl.TextField = "Text";
            //ddl.ValueField = "Value";
            //ddl.DataBind();
        }
        static IList<TreeViewNode> CreateDropDown<Category>(string propId, string propText, string propValue, string propParent, string propSort, List<Category> list, bool isIdEqValue) where Category : class
        {
            IList<TreeViewNode> TreeList = new List<TreeViewNode>();
            foreach (var obj in list)
            {
                var node = new TreeViewNode();
                //PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(obj);
                var type = obj.GetType();
                var p = type.GetProperty(propText);
                if (p == null) throw new ArgumentException(propText + " is null!");
                node.Text = Convert.ToString(p.GetValue(obj, null));
                p = type.GetProperty(propId);
                if (p == null) throw new ArgumentException(propId + " is null!");
                node.Id = Convert.ToString(p.GetValue(obj, null));
                p = type.GetProperty(propValue);
                if (p == null) throw new ArgumentException(propValue + " is null!");
                node.Value = Convert.ToString(p.GetValue(obj, null));
                if (isIdEqValue) node.Value = node.Id;
                if (!string.IsNullOrEmpty(propParent))
                {
                    p = type.GetProperty(propParent);
                    if (p != null)
                    {
                        var parent = Convert.ToString(p.GetValue(obj, null));
                        node.Parent = string.IsNullOrEmpty(parent) ? null : parent;
                    }
                }
                if (!string.IsNullOrEmpty(propSort))
                {
                    p = type.GetProperty(propSort);
                    if (p != null)
                        node.Sort = Convert.ToInt32(p.GetValue(obj, null));
                }
                TreeList.Add(node);
            }
            if (string.IsNullOrEmpty(propParent)) return TreeList;
            IList<TreeViewNode> resultList = new List<TreeViewNode>();
            BindNode(null, TreeList, 0, resultList);
            return resultList;
        }
        static void BindNode(string code, IList<TreeViewNode> list, int deep, IList<TreeViewNode> resultList)
        {
            string head = "├";
            var clist = list.Where(o => o.Parent == code);
            foreach (var item in clist)
            {
                for (int i = 0; i < deep; i++)
                {
                    head = HttpContext.Current.Server.HtmlDecode("   ") + head;
                }
                item.Text = deep == 0 ? item.Text : head + item.Text;
                head = "├";
                resultList.Add(item);
                BindNode(item.Id, list, deep + 1, resultList);
            }
        }
        static Control FindControl(Control root, string Id)
        {
            if (root == null) return null;
            if (root.ID == Id) return root;
            foreach (Control c in root.Controls)
            {
                var result = FindControl(c, Id);
                if (result != null)
                    return result;
            }
            return null;
        }

效果:


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hebbers/article/details/79972043

DevExpress ASPxComboBox数据绑定问题

是用Devexpress ASPxComboBox,没有定义ListBoxColumn情况下,可以通过                 ddlSchoolTerm.DataSource = cour...
  • jjhua
  • jjhua
  • 2012-04-04 13:04:37
  • 2576

ASPxComboBox绑定值 并输入联想

                                    ValueField="Oid" TextField="Name" runat="server"  Width="96%"   ...
  • goodyatou
  • goodyatou
  • 2011-03-28 15:25:00
  • 4496

在下拉框里面绑定树形显示

  • 2014年06月25日 17:56
  • 1KB
  • 下载

为ASPxComboBox 控件动静态绑定下拉项的方法

1定义提供数据源的方法:protected IEnumerable GetParentRoleDataSource(object categoryId)        {               ...
  • longer123123
  • longer123123
  • 2009-03-18 17:54:00
  • 5061

aspx下拉框和单选框的后台数据绑定

后台 sel.Items.Add(new ListItem(显示到前台的名字,值)),单选框也一样。
  • dubuhuaji
  • dubuhuaji
  • 2014-11-26 14:28:36
  • 1158

ASPxComboBox使用说明

属性:EnableCallbackMode:允许其数据的装载需求从服务器上,通过回调,如果其EnableCallbackMode被设置为真。例如,列表条目显示在此以前被当前不编辑的下拉列表窗口,可动态...
  • rbwang
  • rbwang
  • 2011-06-06 17:21:00
  • 5027

DevExpress中的ASPxGridView添加、编辑时的ComboBox联动研究

在学习了一段时间DevExpress后,无论是MVC下的ASPxGridView,还是WebForm下的都有了一定的了解,不过最近在webform下使用下拉框联动时还是碰到了一定的问题,研究了官方De...
  • hui000444
  • hui000444
  • 2012-04-26 18:29:58
  • 2225

基于JavaScript实现树形下拉框

的小伙伴们可以参考一下 平时会经常遇到树形结构的问题,比如显示目录结构等。 大多数情况下后台会返回这样的数据,如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13...
  • mixi9760
  • mixi9760
  • 2016-11-09 20:30:00
  • 1349

jquery ztree实现下拉树形框,json数据

公司最近的项目需要用到树形下拉框,在网上找了挺多yuan
  • qinshang007
  • qinshang007
  • 2014-05-12 15:52:54
  • 2552

树形和仿树形下拉框

一般来说树形下拉框是这样的 接收的数据格式是这样的 id-pid-children模式 上面这中数据格式可以这样子来 var jsonDB = [     ...
  • qq873113580
  • qq873113580
  • 2016-05-16 15:43:03
  • 457
收藏助手
不良信息举报
您举报文章:实现ASPxComboBox下拉框绑定树形数据
举报原因:
原因补充:

(最多只允许输入30个字)