偶这个前端设计师有生以来写过的最复杂的程序业务逻辑(菜鸟贴)。

05年底写的,八年工作以来第一次以.net程序员而不是设计师的身份应聘到一公司,在试用期间写过的一个不同单位OA系统间的部门、人员建立及数据同步交换流程,包括界面的处理。

反正对我来说,已经很复杂了……当时可谓是绞尽脑汁,倾力而为,精尽人亡(哈哈,见笑了,个人能力有限。)。写完之后因为开始主导Web标准的部署重回到设计师的路上就再也没碰过程序了,不知道以后还会不会写,说不定就成个人在编程上的绝唱了,所以一直保留着的,呵呵。

namespace Winstar.Build.HR.Module
{
    
using System;
    
using System.Data;
    
using System.Data.SqlClient;
    
using System.Drawing;
    
using System.Xml;
    
using System.Web;
    
using System.Web.UI.WebControls;
    
using System.Web.UI.HtmlControls;

    
using Microsoft.Web.UI.WebControls;
    
using Winstar.Build.HR.BusinessFacade;
    
using Winstar.Build.HR.Controls;
    
using Winstar.Build.HR.Common;
    
using Winstar.Build.HR.DataAccess;
    
using Winstar.Build.HR.WebUI.Control;
    
using Winstar.IACenterAD.WebService.Agent;

    
/// <summary>
    
///        HR_InitializeOrganization 的摘要说明。
    
/// </summary>
    public class HR_InitializeOrganization :Winstar.Framework.WebUI.PortalModuleControl
    {
        
protected Microsoft.Web.UI.WebControls.TreeView TreeViewHR;
        
protected System.Web.UI.WebControls.TextBox OrganizationName;
        
protected System.Web.UI.WebControls.TextBox OrganizationOaid;
        
protected System.Web.UI.WebControls.Label OrganizationID;
        
protected System.Web.UI.WebControls.Label lblDelTip;
        
protected System.Web.UI.WebControls.Label lblError;
        
protected System.Web.UI.WebControls.DropDownList DDLOrganizationType;
        
protected System.Web.UI.HtmlControls.HtmlGenericControl ToolBar;
        
protected System.Web.UI.HtmlControls.HtmlAnchor Organization_Synchronize;
        
protected System.Web.UI.HtmlControls.HtmlAnchor Organization_Add;
        
protected System.Web.UI.HtmlControls.HtmlAnchor Organization_Save;
        
protected System.Web.UI.HtmlControls.HtmlAnchor Organization_Del;
        
protected System.Web.UI.HtmlControls.HtmlAnchor Organization_Back;
        
protected System.Web.UI.HtmlControls.HtmlAnchor upBtnGroup;
        
protected System.Web.UI.HtmlControls.HtmlAnchor downBtnGroup;
        
public System.Web.UI.HtmlControls.HtmlGenericControl OrganizationDataArea;
        
protected System.Web.UI.HtmlControls.HtmlGenericControl DelResultArea;
        
protected System.Web.UI.HtmlControls.HtmlGenericControl SynchronizeTipArea;

        
public DataTable ReportDT;
        
        CollectionTable ct 
= new CollectionTable();
        
public string id, type, stradd, strSynchronize, strSyn, strMorS, strDel, strStyle;
        
public int personcount, departmentcount;
        
public string[]  strcondition;
        
string selectedtype;
        
public string organizationid, organizationname, organizationoaid, organizationtype;
        

        
public void Page_Load(object sender, System.EventArgs e)
        {
            
this.Page.SmartNavigation = true;
            TreeViewHR.AutoPostBack 
= true;

            
string strTreeName = "_ctl1_TreeViewHR";
            
string strRef = Page.GetPostBackEventReference(TreeViewHR);
            
string strScript = "<script language=\"JavaScript\"> \n" + "<!-- \n" + " function initTree() { \n" +" " + strTreeName + ".onSelectedIndexChange = function() { \n" + "if (event.oldTreeNodeIndex != event.newTreeNodeIndex) \n" + "this.queueEvent('onselectedindexchange', event.oldTreeNodeIndex + ',' + event.newTreeNodeIndex); \n" + "window.setTimeout('" + strRef.Replace("'","\\'"+ "', 0, 'JavaScript'); \n" + " } \n" + " } \n" + "// --> \n" + "</script>"
            Page.RegisterClientScriptBlock(
"InitTree",strScript ); 

            
//初始化HTML控件显示属性

            OrganizationDataArea.Visible 
= false;
            SynchronizeTipArea.Visible 
= false;
            DelResultArea.Visible 
= false;
            lblError.Visible 
= false;

            Organization_Add.Visible 
= false;
            Organization_Save.Visible 
= false;
            Organization_Del.Visible 
= false;
            Organization_Back.Visible 
= false;

            
if (!IsPostBack)
            {
                stradd 
= "新建单位";
                strSynchronize 
= "初始化";
                strSyn 
= "同步";
                ViewState[
"add"= stradd;
                ViewState[
"Synchronize"= strSynchronize;
                Organization_Synchronize.Visible 
= true;
                Organization_Add.Visible 
= true;
                InitTreeByXml();
            }
        }

        
#region 建立TreeView并初始化。
        
private void InitTreeByXml()
        {            
            TreeViewHR.Nodes.Clear();
            TreeNode noderoot 
= new TreeNode();
            noderoot.Text
="根目录" ;
            noderoot.NavigateUrl 
= "DesktopDefault.aspx?tabid=925f53ab-03bd-4de4-9df5-4abad04ec9c9";
            
this.TreeViewHR.Nodes.Add(noderoot);
            XmlDocument xmldoc 
= new XmlDocument();
            xmldoc.Load(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));
            System.Xml.XmlNodeList xmlChildNodeList 
= xmldoc.SelectNodes("//department[@Type = '1']");
            
for (int i = 0; i < xmlChildNodeList.Count; i++)
            {
                TreeNode node 
= buildTreeByXml(xmlChildNodeList[i]);
                
if (null != node)
                    noderoot.Nodes.Add(node);
            }
        }
        
private TreeNode buildTreeByXml(XmlNode xmlTreeNode)
        {
            TreeNode node 
= null;
            
if (null == xmlTreeNode) return node;
            node 
= new TreeNode();
            node.Text 
= xmlTreeNode.Attributes["Name"].Value;
            node.ID 
= xmlTreeNode.Attributes["ID"].Value;
            node.Type 
= xmlTreeNode.Attributes["Oaid"].Value;
            node.NodeData 
= xmlTreeNode.Attributes["Type"].Value;
            
            XmlNodeList nodelist 
= xmlTreeNode.ChildNodes;
            
for (int i = 0; i < nodelist.Count; i++)
                node.Nodes.Add(buildTreeByXml(nodelist[i]));
            
return node;
        }
        
#endregion

        
#region 同步OA系统部门及人员数据

        
#region 同步流程
        
public void SynchronizeFlow(string[] strcondition, string type)
        {
            XmlNode newnode;
            Organize IAagent
=new Organize(); 
            newnode 
= IAagent.GetOrganizeInfo("","");
            
//XmlDocument xmldoc = new XmlDocument();                    
            
//xmldoc.Load(this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"Organization.xml"));              
            for ( int i=0; i < strcondition.Length; i++)
            {
                XmlNodeList OrganizationNodeList 
= newnode.SelectNodes("//department[@type = '" + type + "'" + strcondition[i] + "]");
                SynchronizeOrganization(OrganizationNodeList, type);
            }
        }
        
#endregion

        
#region 同步数据库机构信息
        
void SynchronizeOrganization(XmlNodeList OrganizationNodeList, string type)
        {
            
string[] result = new string[]{""};
            
string[,] resultvalue = new string[OrganizationNodeList.Count, 2];

            
#region 同步机构。
            
for (int i = 0; i < OrganizationNodeList.Count; i++)
            {
                
//建立需要传入过程的一,二维单位参数数组(由OA系统XML文档取得机构数据)||(由取得前台页面输入的新机构数据)
                string[] arrParameterOrganization = new string[]
                    {
                        System.Guid.NewGuid().ToString(),
                        OrganizationNodeList[i].Attributes[
"name"].Value,
                        OrganizationNodeList[i].Attributes[
"id"].Value
                    };
                
string[,] arrParameterUnit = new string[,]
                    {
                        {
"unitauthorizedguid","SqlDbType.VarChar, 50"},
                        {
"unitname""SqlDbType.VarChar, 200"},
                        {
"unitid""SqlDbType.VarChar, 50"}
                    };

                
if ( type == "1" )
                {
                    
#region 取到数据为单位数据
                    
//将各参数传入数据层,返回操作结果。
                    SqlDataAccess getresult = new SqlDataAccess();
                    result 
= getresult.GetResult(arrParameterOrganization, arrParameterUnit, "HR_Handle_Unit"3);

                    
//建立或更新XML单位节点。
                    SynchronizeLocalXmlDoc(OrganizationNodeList[i]);
                    
//查询OA系统XML当前单位节点下的所有部门。||若是建立新单位,则以下查询结果=null,循环不被执行。
                    XmlNodeList DepartmentNodeList = OrganizationNodeList[i].SelectNodes("*");

                    
#region 同步部门及其人员(循环)
                    
for ( int j = 0; j < DepartmentNodeList.Count; j++)
                    {
                        
//在该单位节点下建立部门节点。
                        SynchronizeLocalXmlDoc(DepartmentNodeList[j]);
                        
//同步人员信息。
                        SynchronizePerson(DepartmentNodeList[j]);
                    }
                    
#endregion
                    
                    
#endregion
                }
                
else
                {
                    
#region 取到数据为部门数据
                    
//更新本地XML该部门数据。
                    SynchronizeLocalXmlDoc(OrganizationNodeList[i]);
                    
if ( type != "10" )
                    {
                        
//传入的NodeList为OA系统部门信息,执行人员同步操作。(若type=10则传入为新建立部门数据,不执行人员同步)
                        SynchronizePerson(OrganizationNodeList[i]);
                    }
                    
#endregion
                }
            }
            
#endregion

            
//rptOrganizationResult.DataSource = resultvalue;
            
//rptOrganizationResult.DataBind();
        }
        
#endregion

        
#region 同步数据库人员信息
        
public void SynchronizePerson(XmlNode DepartmentNode)
        {
            
//查询OA系统XML当前部门节点下的所有人员。
            XmlNodeList PersonNodeList = DepartmentNode.SelectNodes("members/member");
            
string[,] resultvalue = new string[PersonNodeList.Count,2];
            
//取得OA系统XML当前人员节点中相关属性。

            
#region 同步人员(循环)
            
for ( int i = 0; i < PersonNodeList.Count; i++ )
            {
                
string personnelguid = System.Guid.NewGuid().ToString();
                
string guid = personnelguid;
                
string personname = PersonNodeList[i].Attributes["name"].Value;
                
string personid = PersonNodeList[i].Attributes["id"].Value;
                
string oaccount = PersonNodeList[i].Attributes["account"].Value;
                
string departmentid = PersonNodeList[i].ParentNode.ParentNode.Attributes["id"].Value;
                
string telephone = PersonNodeList[i].Attributes["tel"].Value;
                
string mobile = PersonNodeList[i].Attributes["mobile"].Value;
                
string departmentname = PersonNodeList[i].ParentNode.ParentNode.Attributes["name"].Value;
                
string unitid = PersonNodeList[i].ParentNode.ParentNode.ParentNode.Attributes["id"].Value;
                
if ( unitid == "0501021148366051" )
                {
                    unitid 
= PersonNodeList[i].ParentNode.ParentNode.Attributes["id"].Value;
                    departmentid
="";
                    departmentname
="";
                }
                
string iorder = PersonNodeList[i].Attributes["usernumber"].Value;

                
//建立需要传入过程的一,二维人员参数数组
                string[] arrParameter = new string[]
                                {
                                    personnelguid,
                                    guid,
                                    personname,
                                    personid,
                                    oaccount,
                                    departmentid,
                                    telephone,
                                    mobile,
                                    departmentname,
                                    unitid,
                                    iorder
                                };
                
string[,] arrParameterPerson = new string[,]
                                {
                                    {
"personnelguid""SqlDbType.VarChar, 50"},
                                    {
"guid""SqlDbType.VarChar, 50"},
                                    {
"personname""SqlDbType.VarChar, 50"},
                                    {
"personid""SqlDbType.VarChar, 50"},
                                    {
"oaccount""SqlDbType.VarChar, 50"},
                                    {
"departmentid""SqlDbType.VarChar, 50"},
                                    {
"telephone""SqlDbType.VarChar, 50"},
                                    {
"mobile""SqlDbType.VarChar, 50"},
                                    {
"departmentname""SqlDbType.VarChar, 200"},
                                    {
"unitid""SqlDbType.VarChar, 50"},
                                    {
"iorder""SqlDbType.VarChar, 50"}
                                };
                
//将各参数传入数据层,返回操作结果。
                SqlDataAccess getresult = new SqlDataAccess();
                
string[] result = getresult.GetResult(arrParameter, arrParameterPerson, "HR_Handle_Person"11);
            }
            
#endregion

        }
        
#endregion

        
#region 同步本地XML文档
        
void SynchronizeLocalXmlDoc(XmlNode OrganizationNode)
        {
            XmlDocument localxmldoc 
= new XmlDocument();
            localxmldoc.Load(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));
            XmlNode LocalNode 
= null;

            
#region 狗屎程序故障,见鬼了。
            
//            if ( OrganizationNode.Attributes["id"] == null)
            
//            {
            
//                Response.Write("0");
            
//            }
            
//            else
            
//            {Response.Write("1");}
            #endregion
            
            
if ( OrganizationNode.Attributes["id"!= null )
            {
                LocalNode 
= localxmldoc.SelectSingleNode("//department[@ID = '" + OrganizationNode.Attributes["id"].Value + "']");
                
if ( LocalNode == null )
                {
                    
//本地XML文档中无该节点,建立。    
                    if( OrganizationNode.ParentNode.Attributes["id"== null )
                    {
                        
//该节点为单位节点
                        LocalNode = localxmldoc.DocumentElement;
                    }
                    
else
                    {
                        
//该节点为部门节点
                        LocalNode = localxmldoc.SelectSingleNode("//department[@ID = '" + OrganizationNode.ParentNode.Attributes["id"].Value + "']");
                    }
                    XmlElement node 
= localxmldoc.CreateElement("department");

                    node.SetAttribute(
"ID",OrganizationNode.Attributes["id"].Value.ToString());
                    node.SetAttribute(
"Name",OrganizationNode.Attributes["name"].Value.ToString());
                    
if ( OrganizationNode.Attributes["oaid"== null)
                    {
                        node.SetAttribute(
"Oaid",OrganizationNode.Attributes["id"].Value.ToString());
                    }
                    
else
                    {
                        node.SetAttribute(
"Oaid",OrganizationNode.Attributes["oaid"].Value.ToString());
                    }
                    node.SetAttribute(
"Type",OrganizationNode.Attributes["type"].Value.ToString());
                
                    LocalNode.AppendChild(node); 
                    localxmldoc.Save(
this.Page.Server.MapPath( Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml")); 
                }
                
else
                {
                    
//更新节点
                    LocalNode.Attributes["Name"].Value = OrganizationNode.Attributes["name"].Value;
                    LocalNode.Attributes[
"ID"].Value = OrganizationNode.Attributes["id"].Value;
                    
if ( OrganizationNode.Attributes["oaid"!= null )
                    {
                        LocalNode.Attributes[
"Oaid"].Value = OrganizationNode.Attributes["oaid"].Value;
                    }
                    
else
                    {
                        LocalNode.Attributes[
"Oaid"].Value = OrganizationNode.Attributes["id"].Value;
                    }
                    LocalNode.Attributes[
"Type"].Value = OrganizationNode.Attributes["type"].Value;
                    localxmldoc.Save(
this.Page.Server.MapPath( Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml")); 
                }
            }
        }
        
#endregion

        
#region 处理特例直属单位:存入直属单位下所有单位数据到数据库,并删除直属单位
        
public void ReplaceDBUnit()
        {
            
string[] result = new string[]{""};

            XmlDocument localxmldoc 
= new XmlDocument();
            localxmldoc.Load(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));
            
            XmlNodeList NodeList 
= localxmldoc.DocumentElement.SelectSingleNode("//department[@ID = '0501021148366051']").SelectNodes("*");

            
for ( int i = 0; i < NodeList.Count; i++ )
            {
                
string[] arrParameterOrganization = new string[]
                    {
                        NodeList[i].Attributes[
"ID"].Value,
                        NodeList[i].Attributes[
"Name"].Value,
                        NodeList[i].Attributes[
"Oaid"].Value,
                    };
                
string[,] arrParameterUnit = new string[,]
                    {
                        {
"unitauthorizedguid","SqlDbType.VarChar, 50"},
                        {
"unitname""SqlDbType.VarChar, 200"},
                        {
"unitid""SqlDbType.VarChar, 50"}
                    };

                SqlDataAccess getresult 
= new SqlDataAccess();
                result 
= getresult.GetResult(arrParameterOrganization, arrParameterUnit, "HR_Handle_Unit"3);
            }
            SqlDataAccess delresult 
= new SqlDataAccess();
            personcount 
= delresult.DeleteOrganization("0501021148366051""delete");
        }
        
#endregion

        
#endregion

        
#region 绑定相应XML数据
        
private void BindXmlData(string id, string name, string oaid, string type)
        {
            DDLOrganizationType.Items.Clear();

            OrganizationName.Text 
= name;
            OrganizationID.Text 
= id;
            OrganizationOaid.Text 
= oaid;
            
//判断生成DDL
            if ( type == "1")
                {DDLOrganizationType.Items.Add(
new ListItem("单位""unit"));}
            
else if ( type == "2" )
                {DDLOrganizationType.Items.Add(
new ListItem("部门""department"));}
            
else
                {DDLOrganizationType.Items.Add(
new ListItem("领导部门""leaddepartment"));}
        }
        
#endregion

        
#region 树点击事件
        
public void TreeViewHR_SelectedIndexChange(object sender, Microsoft.Web.UI.WebControls.TreeViewSelectEventArgs e)
        {
            TreeNode node 
= TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex);
            organizationtype 
= node.NodeData;
            organizationname 
= node.Text;
            organizationoaid 
= node.Type;
            organizationid 
= node.ID;

            ViewState[
"type"= organizationtype;
            ViewState[
"name"= organizationname;
            ViewState[
"id"= organizationid;
            ViewState[
"oaid"= organizationoaid;

            strMorS 
= "修改";
            ViewState[
"MorS"= strMorS;
            strDel 
= "删除";
            ViewState[
"Del"= strDel;

            node.Expanded 
= true;

            BindXmlData(organizationid, organizationname, organizationoaid, organizationtype);

            Organization_Synchronize.Visible 
= true;
            OrganizationDataArea.Visible 
= true;
            Organization_Add.Visible 
= true;
            Organization_Save.Visible 
= true;
            Organization_Del.Visible 
= true;
            

            
if ( organizationtype == "1" )
            {
                stradd 
= "新建部门或者领导部门";
                ViewState[
"add"= stradd;
            }
            
else
            {
                stradd 
= "新建子部门";
                ViewState[
"add"= stradd;
            }

            strSynchronize 
= "同步更新";
            ViewState[
"Synchronize"= strSynchronize;
        }
        
#endregion

        
#region 删除单位或者部门
        
private void DeleteOrganization(string id, string mark)
        {
            
//将参数传入数据层,返回操作结果。
            SqlDataAccess delresult = new SqlDataAccess();
            personcount 
= delresult.DeleteOrganization(id, mark);

            XmlDocument localxmldoc 
= new XmlDocument();
            localxmldoc.Load(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));
            XmlNode Node 
= localxmldoc.SelectSingleNode("//department[@ID = '" + id + "']");

            
if ( mark == "getdelinfo")
            {
                departmentcount 
= Node.ChildNodes.Count;
            }
            
else
            {
                XmlNode NodeParent 
= Node.ParentNode;
                NodeParent.RemoveChild(Node);
                localxmldoc.Save(
this.Page .Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml")); 
            }
        }
        
#endregion

        
#region 按钮

        
#region 同步
        
public void Organization_Synchronize_Click(object sender, EventArgs e)
        {
            
if ( ViewState["add"].ToString() == "新建单位" )
            {
                
// 根目录页面
                strcondition = new string[]
                    {
                        
"and @id=05010211483776592",
                        
"and @id=0501021148366051"
                    };
                
if ( ViewState["Synchronize"].ToString() == "初始化" )
                {
                    
// 点击初始化按钮后。
                    strSynchronize = "确定初始化";
                    ViewState[
"Synchronize"= strSynchronize;
                    organizationname 
= "提示:同步机构<span>杭州市XXXX单位</span>和<span>杭州市XXX委员会</span>中所有部门信息以及各部门下所有人员信息,无则增加,有则更新以保持与OA系统数据一致性。是否确定?";
                    Organization_Add.Visible 
= false;
                    Organization_Back.Visible 
= true;

                    SynchronizeTipArea.Visible 
= true;
                }
                
else
                {
                    
// 点击确定初始化按钮后。
                    strSynchronize = "初始化";
                    ViewState[
"Synchronize"= strSynchronize;
                    stradd 
= ViewState["add"].ToString();
                    Organization_Add.Visible 
= true;
                    Organization_Back.Visible 
= false;
                    
//传参数到同步流程方法中,同步OA。
                    SynchronizeFlow(strcondition, "1");
                    
//处理特例——直属单位
                    ReplaceDBUnit();
                    
//重载树
                    InitTreeByXml();
                }

                Organization_Save.Visible 
= false;
                Organization_Del.Visible 
= false;
            }
            
else
            {
                
// 非根目录页面
                
// 查找本地节点,判断OAID。
                XmlDocument localxmldoc = new XmlDocument();
                localxmldoc.Load(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));
                XmlNode node 
= localxmldoc.SelectSingleNode("//department[@Oaid = '" + ViewState["id"].ToString() + "']");

                
string str = "and @id = " + ViewState["id"].ToString();
                strcondition 
= new string[]
                    {
                        str
                    };

                
if ( ViewState["Synchronize"].ToString() == "同步更新" )
                {
                    
// 点击同步更新按钮后。
                    if ( node != null )
                    {
                        strSynchronize 
= "确定同步";
                        ViewState[
"Synchronize"= strSynchronize;
                        organizationname 
= "提示:同步机构<span>" + ViewState["name"].ToString() + "</span>中所有部门信息以及各部门下所有人员信息,无则增加,有则更新以保持与OA系统数据一致性。是否确定?";
                    }
                    
else
                    {
                        Organization_Synchronize.Visible 
= false;
                        organizationname 
= "提示:机构<span>" + ViewState["name"].ToString() + "</span>为自建机构,在OA系统中不存在,无法进行同步,请返回。";
                    }
                    Organization_Add.Visible 
= false;
                    Organization_Back.Visible 
= true;
                    Organization_Save.Visible 
= false;
                    Organization_Del.Visible 
= false;

                    SynchronizeTipArea.Visible 
= true;
                }
                
else
                {
                    
// 点击确定同步按钮后。
                    strSynchronize = "同步更新";
                    strMorS 
= "修改";
                    ViewState[
"Synchronize"= strSynchronize;
                    stradd 
= ViewState["add"].ToString();
                    OrganizationDataArea.Visible 
= true;
                    Organization_Add.Visible 
= true;
                    Organization_Back.Visible 
= false;
                    Organization_Save.Visible 
= true;
                    Organization_Del.Visible 
= true;
                    
//传参数到同步流程方法中,同步OA。
                    SynchronizeFlow(strcondition, ViewState["type"].ToString());
                    
//处理特例——直属单位
                    ReplaceDBUnit();
                }

            }

            
//SynchronizeFlow(strcondition, "1", "Result");
        }
        
#endregion

        
#region 添加
        
public void Organization_Add_Click(object sender, EventArgs e)
        {
            DDLOrganizationType.Items.Clear();

            strMorS 
= "保存";
            ViewState[
"MorS"= strMorS;
            
//建立页面。
            OrganizationDataArea.Visible = true;
            Organization_Synchronize.Visible 
= false;
            Organization_Save.Visible 
= true;
            Organization_Back.Visible 
= true;

            OrganizationName.Text 
= "";
            OrganizationID.Text 
= System.Guid.NewGuid().ToString();
            OrganizationOaid.Text 
= "0";

            
if ( ViewState["add"].ToString() == "新建单位" )
            {
                DDLOrganizationType.Items.Add(
new ListItem("单位""1"));
            }
            
else
            {
                DDLOrganizationType.Items.Add(
new ListItem("部门""2"));
                DDLOrganizationType.Items.Add(
new ListItem("领导部门""3"));
                DDLOrganizationType.SelectedValue 
= "2";
            }
        }
        
#endregion

        
#region 保存或修改
        
public void Organization_Save_Click(Object sender, EventArgs e)
        {
            lblError.Visible 
= true;
            
if (OrganizationName.Text == "")
            {
                strMorS 
= ViewState["MorS"].ToString();
                OrganizationDataArea.Visible 
= true;
                Organization_Synchronize.Visible 
= false;
                Organization_Save.Visible 
= true;
                Organization_Back.Visible 
= true;
                lblError.Text 
= "用户名不能为空!";
            }
            
else
            {
                
if ( (ViewState["add"].ToString() == "新建单位" || ViewState["type"].ToString() == "1"&& ViewState["add"].ToString() != "新建部门或者领导部门")
                {
                    
string lsxml="<?xml version=\"1.0\" encoding=\"utf-8\" ?><a></a>";
                    XmlDocument tempxmldoc 
= new XmlDocument();
                    tempxmldoc.LoadXml(lsxml);
                    XmlNode LocalNode 
= tempxmldoc.DocumentElement;
                    XmlElement node 
= tempxmldoc.CreateElement("department");

                    node.SetAttribute(
"id", OrganizationID.Text);
                    node.SetAttribute(
"name", OrganizationName.Text);
                    node.SetAttribute(
"oaid", OrganizationOaid.Text);
                    node.SetAttribute(
"type""1");
                
                    LocalNode.AppendChild(node); 

                    XmlNodeList OrganizationNodeList 
= tempxmldoc.SelectNodes("//a/department");
                    SynchronizeOrganization(OrganizationNodeList, 
"1");
                }
                
else
                {
                    
string lsxml="<?xml version=\"1.0\" encoding=\"utf-8\" ?><a id=\"" + ViewState["id"].ToString() + "\"></a>";
                    XmlDocument tempxmldoc 
= new XmlDocument();
                    tempxmldoc.LoadXml(lsxml);
                    XmlNode LocalNode 
= tempxmldoc.DocumentElement;
                    XmlElement node 
= tempxmldoc.CreateElement("department");

                    node.SetAttribute(
"id", OrganizationID.Text);
                    node.SetAttribute(
"name", OrganizationName.Text);
                    node.SetAttribute(
"oaid", OrganizationOaid.Text);
                    
if ( ViewState["MorS"].ToString() == "保存" )
                    {
                        node.SetAttribute(
"type", DDLOrganizationType.SelectedValue);
                    }
                    
else
                    {
                        node.SetAttribute(
"type", ViewState["type"].ToString());
                    }
                
                    LocalNode.AppendChild(node); 

                    XmlNodeList OrganizationNodeList 
= tempxmldoc.SelectNodes("//a/department");
                    SynchronizeOrganization(OrganizationNodeList, 
"10");
                }
                
                Organization_Synchronize.Visible 
= false;
                OrganizationDataArea.Visible 
= true;
                Organization_Back.Visible 
= true;
                lblError.Text 
= "机构" + OrganizationName.Text + " 已保存,请返回。";
            }

            
//重载树
            InitTreeByXml();
        }
        
#endregion

        
#region 删除
        
public void Organization_Del_Click(Object sender, EventArgs e)
        {

            DelResultArea.Visible 
= true;
            Organization_Del.Visible 
= true;
            Organization_Back.Visible 
= true;

            
if ( ViewState["Del"].ToString() == "删除" )
            {
                DeleteOrganization(ViewState[
"id"].ToString(), "getdelinfo");
                
if ( personcount == 0 )
                {
                    strDel 
= "确定删除";
                    ViewState[
"Del"= strDel;
                    lblDelTip.Text 
= "该机构下有部门共 <span>" + departmentcount + "</span> 个,是否确定删除?";
                }
                
else
                {
                    Organization_Del.Visible 
= false;
                    lblDelTip.Text 
= "对不起,该机构下有人员共 <span>" + personcount + "</span> 个,您不能执行删除操作。";
                }
            }
            
else
            {
                DeleteOrganization(ViewState[
"id"].ToString(), "delete");

                stradd 
= "新建单位";
                ViewState[
"add"= stradd;
                strSynchronize 
= "初始化";

                DelResultArea.Visible 
= false;
                Organization_Synchronize.Visible 
= true;
                Organization_Add.Visible 
= true;
                Organization_Del.Visible 
= false;
                Organization_Back.Visible 
= false;

                InitTreeByXml();
            }
        }
        
#endregion

        
#region 返回
        
public void Organization_Back_Click(Object sender, EventArgs e)
        {
            DDLOrganizationType.Items.Clear();

            strMorS 
= "修改";
            ViewState[
"MorS"= strMorS;
            
//建立页面。
            stradd = ViewState["add"].ToString();
            strSynchronize 
= "同步更新";
            OrganizationDataArea.Visible 
= true;
            Organization_Synchronize.Visible 
= true;
            Organization_Add.Visible 
= true;
            Organization_Save.Visible 
= true;
            Organization_Del.Visible 
= true;
            
if ( ViewState["add"].ToString() == "新建部门或者领导部门" )
            {
                OrganizationName.Text 
= ViewState["name"].ToString();
                OrganizationID.Text 
= ViewState["id"].ToString();
                OrganizationOaid.Text 
= ViewState["oaid"].ToString();
                DDLOrganizationType.Items.Add(
new ListItem("子部门""2"));
            }
            
else if ( ViewState["add"].ToString() == "新建子部门" )
            {
                OrganizationName.Text 
= ViewState["name"].ToString();
                OrganizationID.Text 
= ViewState["id"].ToString();
                OrganizationOaid.Text 
= ViewState["oaid"].ToString();
                DDLOrganizationType.Items.Add(
new ListItem("子部门""2"));
            }
            
else
            {
                stradd 
= "新建单位";
                ViewState[
"add"= stradd;
                strSynchronize 
= "初始化";

                OrganizationDataArea.Visible 
= false;
                Organization_Save.Visible 
= false;
                Organization_Del.Visible 
= false;
            }
        }
        
#endregion

        
#region 上移
        
public void upBtnGroup_Click(object sender, EventArgs e)
        {
            ViewState[
"new"]="none";
            
if  (TreeViewHR.SelectedNodeIndex!="0"
            {
                
string strnodeindex = TreeViewHR.SelectedNodeIndex;
                
string strnodeindextemp= TreeViewHR.SelectedNodeIndex;
                
int  li_flag = strnodeindex.LastIndexOf(".");
                XmlDocument xmldoc
=new XmlDocument();
                xmldoc.Load(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));
                TreeNode node
=new TreeNode();
                node
=(TreeNode)(TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).Parent);
                
if  (node.Nodes[0].Text==(TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).Text)) 
                {
                    
return;
                }                    
//上移
                else
                {
                    
int ctri = Convert.ToInt16(strnodeindex.Substring(li_flag + 1))-1;
                    strnodeindex 
= strnodeindex.Substring(0,li_flag+1);    
                    strnodeindex 
= strnodeindex + ctri.ToString();
                    XmlNode xmlparentnode
=null;
                    XmlNode xmlcurrentnode
=null;
                    XmlNode xmlcurrentformanode
=null;
                    
if  (node.GetNodeIndex()=="0"
                    {
                        xmlparentnode
=xmldoc.SelectSingleNode("organizes"); 
                        xmlcurrentnode
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).ID.ToString() +"']"); 
                        xmlcurrentformanode
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(strnodeindex).ID.ToString()+"']"); 
                    }
                    
else
                    {
                        xmlparentnode
=xmldoc.SelectSingleNode("//department[@ID='"+node.ID.ToString()+"']"); 
                        xmlcurrentnode
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).ID.ToString()+"']"); 
                        xmlcurrentformanode 
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(strnodeindex).ID.ToString() +"']");                  
                    
                    }
                    
if (xmlparentnode!=null && xmlcurrentnode!=null && xmlcurrentformanode!=null)
                    {
                        xmlparentnode.InsertBefore(xmlcurrentnode,xmlcurrentformanode);
                        xmldoc.Save(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));     
                        
//根据HR_Organization.xml中的内容生成树型目录        
                        InitTreeByXml();    
                    
/*    if  (TreeViewHR.Nodes.Count>=1) 
                        {
                            TreeViewHR.SelectedNodeIndex=strnodeindex; 
                                
                        }                          
*/
                    }  
                }
            
            }        

        }
        
#endregion

        
#region 下移
        
public void downBtnGroup_Click(object sender, EventArgs e)
        {
            ViewState[
"new"]="none";
            
if  (TreeViewHR.SelectedNodeIndex!="0"
            {
                
string strnodeindex = TreeViewHR.SelectedNodeIndex;
                
string strnodeindextemp= TreeViewHR.SelectedNodeIndex;
                
int  li_flag = strnodeindex.LastIndexOf(".");
                XmlDocument xmldoc
=new XmlDocument();
                xmldoc.Load(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));
                TreeNode node
=new TreeNode();
                node
=(TreeNode)(TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).Parent);
                
if  (node.Nodes[node.Nodes.Count-1].Text==(TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).Text))   
                {
                    
return;
                }        
                
else
                {
             
                    
int ctri = Convert.ToInt16(strnodeindex.Substring(li_flag + 1))+1;
                    strnodeindex 
= strnodeindex.Substring(0,li_flag+1);    
                    strnodeindex 
= strnodeindex + ctri.ToString(); 
                    XmlNode xmlparentnode
=null;
                    XmlNode xmlcurrentnode
=null;
                    XmlNode xmlcurrentformanode
=null;
                    
if  (node.GetNodeIndex()=="0"
                    {
                
                        xmlparentnode
=xmldoc.SelectSingleNode("//organizes"); 
                        xmlcurrentnode
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).ID.ToString()+"']"); 
                        xmlcurrentformanode 
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(strnodeindex).ID.ToString()  +"']"); 
                    }
                    
else
                    {                 
                        xmlparentnode
=xmldoc.SelectSingleNode("//department[@ID='"+node.ID.ToString()+"']"); 
                        xmlcurrentnode
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(TreeViewHR.SelectedNodeIndex).ID.ToString()+"']"); 
                        xmlcurrentformanode 
=xmldoc.SelectSingleNode("//department[@ID='"+TreeViewHR.GetNodeFromIndex(strnodeindex).ID.ToString()  +"']");                  
                    }
                    
if (xmlparentnode!=null && xmlcurrentnode!=null && xmlcurrentformanode!=null)
                    {
                        xmlparentnode.InsertAfter(xmlcurrentnode,xmlcurrentformanode);
                        xmldoc.Save(
this.Page.Server.MapPath(Winstar.Build.HR.Common.AppConfig.ConfigPath+"HR_Organization"+".xml"));     
                        
//根据HR_Organization.xml中的内容生成树型目录        
                        InitTreeByXml();    
                    
/*    if  (TreeViewHR.Nodes.Count>=1) 
                        {
                            TreeViewHR.SelectedNodeIndex=strnodeindex;         
                        }                      
*/
                    }  
                }
            }
        }
        
#endregion

        
#endregion

        
#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        {
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }
        
        
/// <summary>
        
///        设计器支持所需的方法 - 不要使用代码编辑器
        
///        修改此方法的内容。
        
/// </summary>
        private void InitializeComponent()
        {
            
this.TreeViewHR.SelectedIndexChange += new Microsoft.Web.UI.WebControls.SelectEventHandler(this.TreeViewHR_SelectedIndexChange);
            
this.Load += new System.EventHandler(this.Page_Load);
        }
        
#endregion
    }
}

原文地址:https://www.cnblogs.com/yuntian/p/666949.html