aspnet自定义控件Treeview基本用法

本文详细介绍了如何在ASP.NET用户控件中实现组织树控件的自定义事件、属性和方法,包括属性如当前组织实体、已选组织列表、显示复选框等,以及事件如节点选择改变事件,还提供了递归绑定组织层级结构的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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/archive/2011/10/24/2222865.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值