Treeview绑定数据源 层叠结构数据源的应用(续--完善篇)
Treeview绑定数据源 层叠结构数据源的应用(续--完善篇)
上次发了一篇有关于使用层叠结构数据源的文章,但那个是有一些小问题的,会影响到正常使用的文章,后来经过研究(当然少不了说一下网友:代码乱了的大力支持),学习别人的代码,后来我终于找到了问题的原因所在。
现在把完成的没有问题的再发一次。
数据库结构:
这个是数据源类,可以放在app_code中去
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entity;
using DataLogic;
/// <summary>
/// 层叠数据源的使用
/// 作者:房客
/// Blog:http://sxlfybb.cnblogs.com
/// </summary>
public class ChannelData : IHierarchyData
{
private List<ChannelInfo> _list = null;
private ChannelInfo _info = null;
public List<ChannelInfo> ChannelInfoList
{
set { _list = value; }
}
public ChannelInfo ChanInfo
{
set { _info = value; }
}
public ChannelData(ChannelInfo info, List<ChannelInfo> list)
{
_list = list;
_info = info;
}
public bool HasChildren
{
get
{
foreach (ChannelInfo info in _list)
{
if (info.PID == _info.CID)
return true;
}
return false;
}
}
public string Path
{
get { return _info.ChannelUrl; }
}
public object Item
{
get
{
return _info;
}
}
public string Type
{
get { return _info.GetType().ToString(); }
}
public IHierarchyData GetParent()
{
foreach (ChannelInfo info in _list)
{
if (info.CID == _info.PID)
return info as IHierarchyData;
}
return null;
}
public IHierarchicalEnumerable GetChildren()
{
List<ChannelInfo> list = new List<ChannelInfo>();
if (_info.CID > 0)
{
foreach (ChannelInfo info in _list)
{
if (info.PID == _info.CID)
{
list.Add(info);
}
}
}
return new HierarchicalCollection(list, _list);
}
}
public class HierarchicalCollection : IHierarchicalEnumerable
{
private List<ChannelInfo> _list = null; //当选集合
private List<ChannelInfo> _allist = null; //所有集合
public List<ChannelInfo> ChannelInfoList
{
set { _list = value; }
}
public HierarchicalCollection(List<ChannelInfo> list, List<ChannelInfo> all)
{
_list = list;
_allist = all;
}
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
//return enumeratedItem as IHierarchyData;
return new ChannelData(enumeratedItem as ChannelInfo, _allist);
}
public System.Collections.IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
}
数据源视图
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entity;
using DataLogic;
/// <summary>
/// 层叠数据源的使用
/// 作者:房客
/// Blog:http://sxlfybb.cnblogs.com
/// </summary>
public class ChannelData : IHierarchyData
{
private List<ChannelInfo> _list = null;
private ChannelInfo _info = null;
public List<ChannelInfo> ChannelInfoList
{
set { _list = value; }
}
public ChannelInfo ChanInfo
{
set { _info = value; }
}
public ChannelData(ChannelInfo info, List<ChannelInfo> list)
{
_list = list;
_info = info;
}
public bool HasChildren
{
get
{
foreach (ChannelInfo info in _list)
{
if (info.PID == _info.CID)
return true;
}
return false;
}
}
public string Path
{
get { return _info.ChannelUrl; }
}
public object Item
{
get
{
return _info;
}
}
public string Type
{
get { return _info.GetType().ToString(); }
}
public IHierarchyData GetParent()
{
foreach (ChannelInfo info in _list)
{
if (info.CID == _info.PID)
return info as IHierarchyData;
}
return null;
}
public IHierarchicalEnumerable GetChildren()
{
List<ChannelInfo> list = new List<ChannelInfo>();
if (_info.CID > 0)
{
foreach (ChannelInfo info in _list)
{
if (info.PID == _info.CID)
{
list.Add(info);
}
}
}
return new HierarchicalCollection(list, _list);
}
}
public class HierarchicalCollection : IHierarchicalEnumerable
{
private List<ChannelInfo> _list = null; //当选集合
private List<ChannelInfo> _allist = null; //所有集合
public List<ChannelInfo> ChannelInfoList
{
set { _list = value; }
}
public HierarchicalCollection(List<ChannelInfo> list, List<ChannelInfo> all)
{
_list = list;
_allist = all;
}
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
//return enumeratedItem as IHierarchyData;
return new ChannelData(enumeratedItem as ChannelInfo, _allist);
}
public System.Collections.IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
}
数据源视图
这个是调用方法:
protected void InitSubMenu()
{
tv_Channel.Target = "mainFrame";
tv_Channel.ShowExpandCollapse = true;
tv_Channel.DataSource = new ChannelDataSource(Channel.GetChannelList());
TreeNodeBinding bind = new TreeNodeBinding();
bind.TextField = "ChannelTitle";
bind.NavigateUrlField = "ChannelUrl";
bind.Target = "mainFrame";
bind.SelectAction = TreeNodeSelectAction.Expand;
tv_Channel.DataBindings.Add(bind);
tv_Channel.DataBind();
TreeNode node = new TreeNode();
node.Text = "退出登录";
node.Value = "0";
node.NavigateUrl = "main.aspx?action=exit";
if (tv_Channel.Nodes.Count > 0)
tv_Channel.Nodes[0].ChildNodes.Add(node);
else
tv_Channel.Nodes.Add(node);
}
这个是treeview显示的效果图:
上次具体出错的原因就先不说了,给你留足够的想象空间。
有问题再留言吧~
好,收工。