public partial class AreaTree : System.Web.UI.UserControl
{
public delegate void ClickHandler(object sender, EventArgs e);//定义一个事件委托
public event ClickHandler Click = null;
private BFLList areaList;
private IQueryable<PUB_C_AREA> Areas;
public List<short?> AreaID
{
get
{
return AreaList();
}
}
public string GetAreaName()
{
return TreeView1.SelectedNode.Text;
}
public void LoadTree()
{
InitTree(TreeView1.Nodes, 0);
}
protected void OnClick(EventArgs e)
{
if (Click != null)
Click(this, e);
}
//递归生成树结点
public void InitTree(TreeNodeCollection Nds, short parentid)
{
areaList = new BFLList();
Areas = areaList.GetAreaAll();
foreach (var area in Areas.Where(p => p.SubOfArea == parentid).Select(p => p))
{
TreeNode node = new TreeNode();
node.Expanded = false;
node.Text = area.NAME;
node.Value = area.AREAID.ToString();
// node.SelectAction = TreeNodeSelectAction.Expand;
Nds.Add(node);
InitTree(Nds[Nds.Count - 1].ChildNodes, area.AREAID);
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadTree();
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
this.OnClick(e);
}
//获取选取树节点及其所有所有子节点
private List<short?> AreaList()
{
List<short?> ShtList = new List<short?>();
areaList = new BFLList();
Areas = areaList.GetAreaAll();
string topArea = TreeView1.SelectedValue;
if (topArea == "all")
{
foreach (var a in Areas)
{
ShtList.Add(a.AREAID);
}
}
else
{
ShtList.Add(short.Parse(topArea));
foreach (var subArea in Areas.Where(p => p.SubOfArea.ToString() == topArea).Select(p => p))
{
ShtList.Add(subArea.AREAID);
recursion(ShtList, subArea.AREAID);
}
}
return ShtList;
}
//递归查找子节点
private void recursion(List<short?> list, short TopID)
{
foreach (var sub in Areas.Where(p => p.SubOfArea == TopID).Select(p => p))
{
list.Add(sub.AREAID);
recursion(list, sub.AREAID);
}
}
}