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>
/// 層疊數據源的使用
/// </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();
}
}
數據源視圖數據源視圖#region 數據源視圖
public class ChannelDataSourceView : HierarchicalDataSourceVIEw
...{
List<ChannelInfo> _list = null;
public List<ChannelInfo> ChannelInfoList
...{
set ...{ _list = value; }
}
public ChannelDataSourceVIEw(List<ChannelInfo> list)
...{
_list = list;
}
public override IHIErarchicalEnumerable Select()
...{
List<ChannelInfo> list = new List<ChannelInfo>();
foreach (ChannelInfo info in _list)
...{
if (info.PID == 0)
list.Add(info);
}
new HIErarchicalCollection(list, _list);
}
}
public class ChannelDataSource : IHIErarchicalDataSource
...{
List<ChannelInfo> _list = null;
public List<ChannelInfo> ChannelInfoList
...{
set ...{ _list = value; }
}
public ChannelDataSource(List<ChannelInfo> 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
這個是調用方法:
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);
&nbs else
tv_Channel.Nodes.Add(node);
}
這個是treevIEw顯示的效果圖: