代码改变世界

ASP.NET组织结构图的画法——数据来源读取数据库

2013-03-21 16:45  愿心平静面对  阅读(798)  评论(0编辑  收藏  举报

今天整整忙了一天,终于给这个组织结构图画出来了

第一:数据库部分:

ID主键ID

Name 组织结构名称

FavID 父组织ID

主要是这三个字段  若是没有父组织,则父组织给FavID=0

第二:添加一个开源的dll:Whidsoft.WebControls.OrgChart.dll下载地址:点击下载控件源码

下载后生成一个dll放到项目中就可以使用了,下面是使用方法

第三:静态使用方法

前台页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<%@ Register TagPrefix="oc" Namespace="Whidsoft.WebControls" Assembly="Whidsoft.WebControls.OrgChart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <style type="text/css">
        .alink a,a:hover,a:visited{
             color:Black;
              text-decoration:none;
        }
    </style>
</head>
<body>
    <form id="Form1" method="post" runat="server">
        <div class="alink">
            <oc:orgchart id="OrgChart1" style="z-index: 101; left: 104px; position: absolute; top: 88px"
                runat="server" LineColor="Silver" Width="100%" Height="100%" ChartStyle="Vertical" ToolTip="test"></oc:orgchart>
        </div>
    </form>
</body>
</html>

 

protected void Page_Load(object sender, EventArgs e)
    {
        string style = Request.QueryString["style"];

            if (style == null || style == "")
            {
                style = "1";
            }

            if (!IsPostBack)
            {
                //每一个组织图中的节点都是一个OrgNode,各属性分别为显示名称,tips
                OrgNode orgNode = new OrgNode();

                orgNode.Text = "罗宝线<br/><span style=\"color:red;\">主管</span>";
                //orgNode.Description =

                orgNode.Description = "这是组织图的塔尖";

                orgNode.NavigateUrl = "http://www.whidsoft.cn";

                orgNode.UnderDesc = "累计电度:1000万度,占线路用电 100%";

                //建立各节点
                OrgNode OrgNode1 = new OrgNode();
                OrgNode1.Text = "车站照明动力";
                OrgNode1.Description = "这是";
                OrgNode1.UnderDesc = "累计电度:55万度,占线路用电 65%";

                OrgNode OrgNode11 = new OrgNode();
                OrgNode11.Text = "车站1";
                OrgNode11.Description = "这是";
                OrgNode11.UnderDesc = "累计电度:33万度,占线路用电 13%";

                OrgNode OrgNode12 = new OrgNode();
                OrgNode12.Text = "车站2";
                OrgNode12.Description = "这是";
                OrgNode12.UnderDesc = "累计电度:133万度,占线路用电 33%";

                OrgNode OrgNode13 = new OrgNode();
                OrgNode13.Text = "车站3";
                OrgNode13.Description = "这是";
                OrgNode13.UnderDesc = "累计电度:13万度,占线路用电 33%";

                OrgNode1.Nodes.Add(OrgNode11);
                OrgNode1.Nodes.Add(OrgNode12);
                OrgNode1.Nodes.Add(OrgNode13);
                orgNode.Nodes.Add(OrgNode1);

                OrgNode OrgNode2 = new OrgNode();
                OrgNode2.Text = "牵引动力";
                OrgNode2.Description = "这是";
                OrgNode2.UnderDesc = "累计电度:13万度,占线路用电 11%";

                OrgNode OrgNode21 = new OrgNode();
                OrgNode21.Text = "1段牵引";
                OrgNode21.Description = "这是";

                OrgNode OrgNode22 = new OrgNode();
                OrgNode22.Text = "2段牵引";
                OrgNode22.Description = "这是";

                OrgNode2.Nodes.Add(OrgNode21);
                OrgNode2.Nodes.Add(OrgNode22);
                orgNode.Nodes.Add(OrgNode2);

                OrgNode OrgNode3 = new OrgNode();
                OrgNode3.Text = "维修段";
                OrgNode3.Description = "这是3";
                OrgNode3.UnderDesc = "累计电度:345万度,占线路用电 54%";

                OrgNode OrgNode31 = new OrgNode();
                OrgNode31.Text = "检修";
                OrgNode31.Description = "这是3";

                OrgNode OrgNode32 = new OrgNode();
                OrgNode32.Text = "洗车及污水";
                OrgNode32.Description = "这是3";

                OrgNode OrgNode33 = new OrgNode();
                OrgNode33.Text = "综合办公楼";
                OrgNode33.Description = "这是3";

                OrgNode3.Nodes.Add(OrgNode31);
                OrgNode3.Nodes.Add(OrgNode32);
                OrgNode3.Nodes.Add(OrgNode33);
                orgNode.Nodes.Add(OrgNode3);

                OrgNode OrgNode4 = new OrgNode();
                OrgNode4.Text = "管理中心";
                OrgNode4.Description = "这是3";
                OrgNode4.UnderDesc = "累计电度:234万度,占线路用电 34%";

                OrgNode OrgNode41 = new OrgNode();
                OrgNode41.Text = "办公";
                OrgNode41.Description = "这是3";

                OrgNode OrgNode42 = new OrgNode();
                OrgNode42.Text = "商业";
                OrgNode42.Description = "这是3";

                OrgNode OrgNode43 = new OrgNode();
                OrgNode43.Text = "机房";
                OrgNode43.Description = "这是3";

                OrgNode4.Nodes.Add(OrgNode41);
                OrgNode4.Nodes.Add(OrgNode42);
                OrgNode4.Nodes.Add(OrgNode43);
                orgNode.Nodes.Add(OrgNode4);

                OrgChart1.Node = orgNode;

                OrgChart1.ChartStyle = (style == "1") ? Whidsoft.WebControls.Orientation.Vertical : Whidsoft.WebControls.Orientation.Horizontal;
            }
    }

或者是动态读取数据库方式,这里前台部分不需要改动,只需要后台些方法就好,主要是一个递归的方法

private void Bind()
    {
        string style = Request.QueryString["style"];

        if (style == null || style == "")
        {
            style = "1";
        }

        //1.数据查询
        Hashtable ht = new Hashtable();
        ht.Add("FavHROID", 0);
        IList<HROrganization> HROLst = HROrganizationService.GetInstance().FindAll(ht);
        OrgNode orgNode = new OrgNode();
        orgNode.Text = HROLst[0].Name;
        
        Hashtable ht1 = new Hashtable();
        ht1.Add("FavHROID", HROLst[0].ID);

        AddNode(ht1, orgNode);
        OrgChart1.Node = orgNode;

        OrgChart1.ChartStyle = (style == "1") ? Whidsoft.WebControls.Orientation.Vertical : Whidsoft.WebControls.Orientation.Horizontal;
        
    }
    /// <summary>
    /// 给节点赋值
    /// </summary>
    /// <param name="ht1"></param>
    /// <param name="orgNode"></param>
    private void AddNode(Hashtable ht1, OrgNode orgNode)
    {
        IList<HROrganization> HROLst2 = HROrganizationService.GetInstance().FindAll(ht1);
        foreach (HROrganization item2 in HROLst2)
        {
            OrgNode OrgNode2 = new OrgNode();
            OrgNode2.Text = item2.Name;
            orgNode.Nodes.Add(OrgNode2);
            Hashtable ht2 = new Hashtable();
            ht2.Add("FavHROID", item2.ID);
            AddNode(ht2, OrgNode2);
        }
    }

希望对大家稍有帮助