动态加载实现无限级树

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Abs.Sys.Model;
using Abs.Sys.Utility.Authorization;
using Sinopec.IWMS.Business;

public partial class ProjectPlan_CommonPage_Organization : System.Web.UI.Page
{
    /// <summary>
    /// 都改成本地库了(2011-06-29)
    /// </summary>
    private readonly string DATAOJBECT_GROUP_ID = "B28E12B0-E375-4D97-95E8-8BFF098F9033"; //数据对象中的“业务组织机构ID查询”ID
    //private readonly string BUSINESS_GROUP_ID = "47978AC9-3AEC-4F19-B1B0-93862800BD84"; //业务组织机构树根节点ID
    private readonly string BUSINESS_GROUP_ID = "E3796A1C-3C42-433B-82B0-12FF6B2DEF62"; //业务组织机构树根节点ID,资产

    /// <summary>
    /// 显示树的方式
    /// 1、等于1时根据当前传入的行政组织机构ID构建树
    /// 2、等于2时根据当前传入的节点展示其下所有节点构建树
    /// 3、默认为显示所有节点
    /// </summary>
    private string showOrgType = string.Empty;
    /// <summary>
    /// 需要显示的机构节点父ID(行政组织机构)
    /// </summary>
    private string orgId = string.Empty;
    /// <summary>
    /// 需要显示的机构节点父ID(业务组织机构)
    /// </summary>
    private string bOrgId = string.Empty;
    /// <summary>
    /// 树节点需要显示到的深度
    /// </summary>
    private int showDepth = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        showDepth = 2;
        showOrgType = Request.QueryString["OrgType"];
        orgId = Request.QueryString["OrgId"];
        bOrgId = Request.QueryString["BOrgId"];

        if (!IsPostBack)
        {
            DataTable dtSource = new DataTable();

            switch (showOrgType)
            {
                case "1":
                    dtSource = BoBasePage.Instance.GetBusinessOrgListByOrgId(orgId); //根据当前传入的行政组织机构ID构建树
                    TreeNode node = new TreeNode();
                    node.Text = "信息化业务组织机构";
                    node.SelectAction = TreeNodeSelectAction.None;
                    this.TreeView1.Nodes.Add(node);
                    foreach (DataRow row in dtSource.Rows)
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Text = row["BusinessName"].ToString();
                        childNode.Value = row["BusinessID"].ToString();
                        childNode.SelectAction = TreeNodeSelectAction.None;
                        node.ChildNodes.Add(childNode);
                    }
                    break;
                case "2":
                    dtSource = BoBasePage.Instance.GetBusinessOrganizationTable(bOrgId); //显示指定结构的树
                    CreateTree(dtSource);
                    break;
                case "3":
                    break;
                default:
                    dtSource = BoBasePage.Instance.GetBusinessOrganizationTable(); //显示所有节点
                    CreateTree(dtSource);
                    break;
            }

        }
        this.Page.Title = "组织机构树";
    }
    private DataTable GetTable(DataTable dtSource, string topid)
    {
        DataTable dtOutPut = dtSource.Clone();
        try
        {
            DataRow[] rows = dtSource.Select("BusinessParentId='" + topid + "'");
            foreach (DataRow row in rows)
            {
                dtOutPut.Rows.Add(row.ItemArray);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        return dtOutPut;
    }
    /// <summary>
    /// 创建组织机构树
    /// </summary>
    /// <param name="dtInputTable"></param>
    private void CreateTree(DataTable dtInputTable)
    {
        try
        {
            if (dtInputTable != null)
            {
                DataTable dtSource = dtInputTable.Clone();
                DataRow[] rows = dtInputTable.Select("BusinessParentId IS NULL OR BusinessParentId = ''");
                bool isExistRoot = false; //是否存在为空的父节点
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        dtSource.Rows.Add(row.ItemArray);
                    }
                    isExistRoot = true;
                }
                else
                {
                    dtSource = dtInputTable;
                }
                if (dtSource != null && dtSource.Rows.Count > 0)
                {
                    int rootNum = 0;
                    if (isExistRoot)
                    {
                        rootNum = dtSource.Rows.Count;
                    }
                    else
                    {
                        rootNum = 1;
                    }
                    for (int i = 0; i < rootNum; i++)
                    {
                        TreeNode tn = new TreeNode();
                        tn.Text = dtSource.Rows[i]["BusinessName"].ToString();
                        tn.Value = dtSource.Rows[i]["BusinessID"].ToString();
                        tn.SelectAction = TreeNodeSelectAction.None;
                        TreeView1.Nodes.Add(tn);
                        CreateNodes(dtInputTable, tn);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    /// <summary>
    /// 添加组织机构节点
    /// </summary>
    /// <param name="dtSource"></param>
    /// <param name="tn"></param>
    private void CreateNodes(DataTable dtSource, TreeNode tn)
    {
        try
        {
            DataTable dtChild = GetTable(dtSource, tn.Value);
            foreach (DataRow row in dtChild.Rows)
            {
                TreeNode node = new TreeNode();
                node.Text = row["BusinessName"].ToString();
                node.Value = row["BusinessID"].ToString();
                node.Expanded = false;
                node.SelectAction = TreeNodeSelectAction.None;
                tn.ChildNodes.Add(node);
                dtChild = dtSource;

                if (tn.Depth != showDepth)
                {
                    //node.ShowCheckBox = false;
                    CreateNodes(dtChild, node);
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        foreach (TreeNode nodeChecked in this.TreeView1.CheckedNodes)
        {
            Response.Write("<script>var u = new Array();u[0]='" + nodeChecked.Text + "';u[1]='" + nodeChecked.Value + "';window.returnValue=u;window.close();</script>");
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        Response.Write("<script>var u = new Array();u[0]='';u[1]='';window.returnValue=u;window.close();</script>");
    }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值