Treeview绑定数据源 层叠结构数据源的应用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
namespace WebApplication1
{
    public class NodeData : IHierarchyData
    {
        private List<Node> _list = null;
        private Node _info = null;
        public List<Node> NodeList
        {
            set { _list = value; }
        }
        public Node ChanInfo
        {
            set { _info = value; }
        }
        public NodeData(Node info, List<Node> list)
        {
            _list = list;
            _info = info;
        }
        public bool HasChildren
        {
            get
            {
                foreach (Node info in _list)
                {
                    if (info.Pid == _info.Id)
                        return true;
                }
                return false;
            }
        }
        public string Path
        {
            get { return _info.Name; }
        }
        public object Item
        {
            get
            {
                return _info;
            }
        }
        public string Type
        {
            get { return _info.GetType().ToString(); }
        }
        public IHierarchyData GetParent()
        {
            foreach (Node info in _list)
            {
                if (info.Id == _info.Pid)
                    return info as IHierarchyData;
            }
            return null;
        }
        public IHierarchicalEnumerable GetChildren()
        {
            List<Node> list = new List<Node>();
            if (_info.Id > 0)
            {
                foreach (Node info in _list)
                {
                    if (info.Pid == _info.Id)
                    {
                        list.Add(info);
                    }
                }
            }
            return new HierarchicalCollection(list, _list);
        }
    }
    public class HierarchicalCollection : IHierarchicalEnumerable
    {
        private List<Node> _list = null; //当选集合
        private List<Node> _allist = null;   //所有集合
        public List<Node> NodeList
        {
            set { _list = value; }
        }
        public HierarchicalCollection(List<Node> list, List<Node> all)
        {
            _list = list;
            _allist = all;
        }
        public IHierarchyData GetHierarchyData(object enumeratedItem)
        {
            //return enumeratedItem as IHierarchyData;
            return new NodeData(enumeratedItem as Node, _allist);
        }                
        public System.Collections.IEnumerator GetEnumerator()
        {
            return _list.GetEnumerator();
        }
    }
    #region 数据源视图
    public class ChannelDataSourceView : HierarchicalDataSourceView
    {
        List<Node> _list = null;
        public List<Node> NodeList
        {
            set { _list = value; }
        }
        public ChannelDataSourceView(List<Node> list)
        {
            _list = list;
        }
        public override IHierarchicalEnumerable Select()
        {
            List<Node> list = new List<Node>();
            foreach (Node info in _list)
            {
                if (info.Pid == 0)
                    list.Add(info);
            }
            return new HierarchicalCollection(list, _list);
        }
    }
    public class ChannelDataSource : IHierarchicalDataSource
    {
        List<Node> _list = null;
        public List<Node> NodeList
        {
            set { _list = value; }
        }
        public ChannelDataSource(List<Node> list)
        {
            if (list == null) throw new NullReferenceException("数据源列表为null,对象没有实例化!");
            _list = list;
        }
        public System.Web.UI.HierarchicalDataSourceView GetHierarchicalView(string viewPath)
        {
            return new ChannelDataSourceView(_list);
        }
        public event EventHandler DataSourceChanged;
    }
    #endregion
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                   
           List<Node> nodes = new List<Node> {
            new Node{ Id=1, Pid=0, Name="China"}       ,
            new Node{ Id=2, Pid=1, Name="Bei jing"},
            new Node{ Id=3, Pid=2, Name="Hai dian"}
           };
                  
           TreeView1.Target = "mainFrame";
           TreeView1.ShowExpandCollapse = true;
           TreeView1.DataSource = new ChannelDataSource(nodes);
           TreeNodeBinding bind = new TreeNodeBinding();
           bind.TextField = "Name";
           bind.NavigateUrlField = "Id";
           bind.Target = "mainFrame";
           bind.SelectAction = TreeNodeSelectAction.Expand;
           TreeView1.DataBindings.Add(bind);
           TreeView1.DataBind();
           TreeNode node = new TreeNode();
           node.Text = "退出登录";
           node.Value = "0";
           node.NavigateUrl = "main.aspx?action=exit";
           if (TreeView1.Nodes.Count > 0)
               TreeView1.Nodes[0].ChildNodes.Add(node);
           else
               TreeView1.Nodes.Add(node);
        }
    }
    public class Node
    {
        public int Id { get; set; }
        public int Pid { get; set; }
        public string Name { get; set; }
    }
}

点击查看原图

知识共享许可协议
《Treeview绑定数据源 层叠结构数据源的应用》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     171.72ms