aspnet自定义控件Treeview基本用法

用户控件添加了自定义事件和属性

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
 
namespace AspNet.UserControl.Organization
{
 public partial class OrganizationTree : System.Web.UI.UserControl
 {
 #region 属?性?
 
 public OrganizationEntity CurrentOrganizationEntity
 {
 get
 {
 if (ViewState["CurrentOrganizationEntity"] != null)
 {
 //ViewState["CurrentOrganizationEntity"] = new T_BD_OrganizationEntity();
 return (OrganizationEntity)ViewState["CurrentOrganizationEntity"];
 }
 else
 {
 //ViewState["CurrentOrganizationEntity"]
 if (this.TVOrganization.Nodes.Count > 0)
 {
 OrganizationEntity entity=new OrganizationEntity();
 entity.OrganizationID = new Guid(this.TVOrganization.Nodes[0].Value);
 return entity;
 }
 }
 return null;
 }
 set { ViewState["CurrentOrganizationEntity"] = value; }
 }
 public List<string> SelectedOrganization
 {
 get
 {
 if (ViewState["SelectedOrganization"] != null)
 {
 return (List<string>)ViewState["SelectedOrganization"];
 }
 if (ShowCheckbox)
 {
 List<string> list = new List<string>();
 // string str = string.Empty;
 foreach (TreeNode item in this.TVOrganization.CheckedNodes)
 {
 if (item.Checked)
 {
 list.Add(item.Value);
 // str += item.Text+";";
 }
 }
 // Label1.Text = str;
 return list;
 }
 return new List<string>();
 }
 set { ViewState["SelectedOrganization"] = value; }
 }
 
 public bool ShowCheckbox
 {
 get
 {
 if (ViewState["ShowCheckbox"] != null)
 {
 return (bool)ViewState["ShowCheckbox"];
 }
 return false;
 }
 set
 {
 ViewState["ShowCheckbox"] = value;
 }
 }
 #endregion 属?性?
 #region 事?件t
 
 public event EventHandler TreeSelectedNodeChangedEvent;//事?件t
 #endregion 事?件t
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!Page.IsPostBack)
 {
  InitTree();
 }
 }
 
 #region 递Y归é绑ó定¨
 private void InitTree()
 {
 TVOrganization.Nodes.Clear();
 if (this.ShowCheckbox)
 {
 TVOrganization.ShowCheckBoxes = TreeNodeTypes.All;
 }
 else
 {
 TVOrganization.ShowCheckBoxes = TreeNodeTypes.None;
 }
 //DataTable dt = GetTreeViewTable();
 OrganizationBLL OrganizationBLL = new  OrganizationBLL();
OrganizationSearchEntity searchEntity = new  OrganizationSearchEntity();
 searchEntity .StateID = 0;
 IList<OrganizationEntity> iList = OrganizationBLL.GetAllEntities(searchEntity );
 
 IList<OrganizationEntity> itemList = iList.Where(p => (p).OrganizationTypeID == 0).ToList<OrganizationEntity>();
 foreach (OrganizationEntity item in itemList)
 {
 
 TreeNode node = new TreeNode();
 
 node.Text = item.OrganizationCName;
 node.Value = item.OrganizationID.ToString();
 TVOrganization.Nodes.Add(node);
 AddTreeNode(iList, node);
 
 }
 
 }
 private void AddTreeNode(IList<OrganizationEntity> list, TreeNode node)
 {
 
 IList<OrganizationEntity> itemList = list.Where(t => ((OrganizationEntity)t).FatherOrganizationID.ToString() == node.Value).ToList<OrganizationEntity>();
 
 foreach (OrganizationEntity item in itemList)
 {
 TreeNode childNode = new TreeNode();
 childNode.Text = item.OrganizationCName;
 childNode.Value = item.OrganizationID.ToString();
 //childNode.Expanded = false;
 node.ChildNodes.Add(childNode);
 AddTreeNode(list, childNode);
 }
 }
 #endregion 递Y归é绑ó定¨
 
 #region Treeview 事?件t
 protected void TVOrganization_SelectedNodeChanged(object sender, EventArgs e)
 {
OrganizationEntity entity = new OrganizationEntity();
 entity.OrganizationID = new Guid(this.TVOrganization.SelectedNode.Value);
 this.CurrentOrganizationEntity = entity;
 if (TreeSelectedNodeChangedEvent != null)
 {
 TreeSelectedNodeChangedEvent(this, e);
 }
 }
 #endregion Treeview 事?件t
 
 #region setCheck/uncheck
 public void SetCheck(List<string> listSource)
 {
 SetUnCheckAll();
 //foreach (string itemStr in listSource)
 //{
 foreach (TreeNode item in TVOrganization.Nodes)
 {
 SetCheckSource(item, listSource);
 // if(item.Value==itemStr)
 //SetCheckbox(item, true);
 }
 //}
 }
 public void SetUnCheckAll()
 {
 foreach (TreeNode item in TVOrganization.Nodes)
 {
 SetCheckbox(item, false);
 }
 
 }
 private void SetCheckbox(TreeNode node,bool check)
 {
 node.Checked = check;
 foreach (TreeNode item in node.ChildNodes)
 {
 item.Checked = check;
 if (item.ChildNodes.Count > 0)
 {
 SetCheckbox(item, check);
 }
 }
 }
 
 private void SetCheckSource(TreeNode node, List<string> listSource)
 {
 foreach (var itemStr in listSource)
 {
 if (node.Value == itemStr)
 {
 node.Checked = true;
 break;
 }
 }
 
 foreach (TreeNode item in node.ChildNodes)
 {
 foreach (var itemStr in listSource)
 {
 if (item.Value == itemStr)
 {
 item.Checked = true;
 break;
 }
 }
 if (item.ChildNodes.Count > 0)
 {
 SetCheckSource(item, listSource);
 }
 }
 }
 #endregion setCheck/uncheck
 
 
 }
}
 
原文地址:https://www.cnblogs.com/z_lb/p/2222865.html