1 VS2005和.NET 2.0
2 Telerik ASP.NET RAD CONTROLS系列控件,安装后会在VS的工具栏里添加一排RAD CONTROL的控件(AJAX版本也不错)
3 castle activerecord ,具体请看terrylee的贴 卢彦的activerecord as tree
4 MYSQL Connector/Net 5.1( MYSQL5.X版本 )
5 NAVICAT 或者 EMS SQL 或者其他MYSQL GUI管理工具
最终实现效果如下
以下为MYSQL 的结构和数据文件
Future Reading:Rails生成ExtTee
2 Telerik ASP.NET RAD CONTROLS系列控件,安装后会在VS的工具栏里添加一排RAD CONTROL的控件(AJAX版本也不错)
3 castle activerecord ,具体请看terrylee的贴 卢彦的activerecord as tree
4 MYSQL Connector/Net 5.1( MYSQL5.X版本 )
5 NAVICAT 或者 EMS SQL 或者其他MYSQL GUI管理工具
源代码里附带了程序源文件,MySQL Connector 5.1,Rad 控件和皮肤
最终实现效果如下
以下为MYSQL 的结构和数据文件
-- ----------------------------
-- Table structure for department_info
-- ----------------------------
DROP TABLE IF EXISTS `department_info`;
CREATE TABLE `department_info` (
`Department_SN` int(11) NOT NULL auto_increment COMMENT '部门流水号',
`Department_ID` varchar(50) default NULL COMMENT '部门编号',
`Department_Name` varchar(50) default NULL COMMENT '部门名称',
`Department_Level` int(11) default NULL COMMENT '部门层级',
`Department_UpperSn` int(11) default NULL COMMENT '部门层级',
`Department_BranchStorea` varchar(50) default NULL COMMENT '上级部门',
`Department_Head` varchar(50) default NULL COMMENT '部门总店',
`Department_Secretary` varchar(50) default NULL COMMENT '部门秘书',
`Department_Contact` varchar(50) default NULL COMMENT '部门联系人',
`Department_Address` varchar(50) default NULL COMMENT '部门地址',
`Department_Tel` varchar(50) default NULL COMMENT '部门电话',
`Department_Fax` varchar(50) default NULL COMMENT '部门传真',
`Department_Status` int(11) default NULL COMMENT '部门状态',
`Department_Memo` varchar(500) default NULL COMMENT '部门备注',
`Department_WBS` varchar(20) default NULL COMMENT 'WBS',
PRIMARY KEY (`Department_SN`)
) ENGINE=InnoDB AUTO_INCREMENT=67 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `department_info` VALUES ('51', '00104', '管理部', '1', null, '1', '00002', '00002', '00002', '上海市龙漕路', '021-6565656', '021-6565656', '1', '', '1');
INSERT INTO `department_info` VALUES ('52', '123123', '财务部', '2', '51', null, null, null, null, null, null, null, null, null, '1.1');
INSERT INTO `department_info` VALUES ('53', '123123', '外场', '3', '52', null, null, null, null, null, null, null, null, null, '1.1.1');
INSERT INTO `department_info` VALUES ('54', '00107', '仓管', '2', '51', null, null, null, null, null, null, null, null, null, '1.3');
INSERT INTO `department_info` VALUES ('55', '00108', '厨房', '4', '53', null, null, null, null, null, null, null, null, null, '1.1.1.1');
INSERT INTO `department_info` VALUES ('56', '00109', '采购', '2', '51', null, null, null, null, null, null, null, null, null, '1.2');
-- Table structure for department_info
-- ----------------------------
DROP TABLE IF EXISTS `department_info`;
CREATE TABLE `department_info` (
`Department_SN` int(11) NOT NULL auto_increment COMMENT '部门流水号',
`Department_ID` varchar(50) default NULL COMMENT '部门编号',
`Department_Name` varchar(50) default NULL COMMENT '部门名称',
`Department_Level` int(11) default NULL COMMENT '部门层级',
`Department_UpperSn` int(11) default NULL COMMENT '部门层级',
`Department_BranchStorea` varchar(50) default NULL COMMENT '上级部门',
`Department_Head` varchar(50) default NULL COMMENT '部门总店',
`Department_Secretary` varchar(50) default NULL COMMENT '部门秘书',
`Department_Contact` varchar(50) default NULL COMMENT '部门联系人',
`Department_Address` varchar(50) default NULL COMMENT '部门地址',
`Department_Tel` varchar(50) default NULL COMMENT '部门电话',
`Department_Fax` varchar(50) default NULL COMMENT '部门传真',
`Department_Status` int(11) default NULL COMMENT '部门状态',
`Department_Memo` varchar(500) default NULL COMMENT '部门备注',
`Department_WBS` varchar(20) default NULL COMMENT 'WBS',
PRIMARY KEY (`Department_SN`)
) ENGINE=InnoDB AUTO_INCREMENT=67 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `department_info` VALUES ('51', '00104', '管理部', '1', null, '1', '00002', '00002', '00002', '上海市龙漕路', '021-6565656', '021-6565656', '1', '', '1');
INSERT INTO `department_info` VALUES ('52', '123123', '财务部', '2', '51', null, null, null, null, null, null, null, null, null, '1.1');
INSERT INTO `department_info` VALUES ('53', '123123', '外场', '3', '52', null, null, null, null, null, null, null, null, null, '1.1.1');
INSERT INTO `department_info` VALUES ('54', '00107', '仓管', '2', '51', null, null, null, null, null, null, null, null, null, '1.3');
INSERT INTO `department_info` VALUES ('55', '00108', '厨房', '4', '53', null, null, null, null, null, null, null, null, null, '1.1.1.1');
INSERT INTO `department_info` VALUES ('56', '00109', '采购', '2', '51', null, null, null, null, null, null, null, null, null, '1.2');
命名空间wbsbom_mysql
VS2008 新建一个WEB站点。按照castle的配置,
需要一个Global.asax,一个实体类文件department_info.cs
web.config的关键设置 2段activerecord设置是重点
Code
gloabax.asax
Code
activeRecord的CRUD
Code
源代码如下
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.WebControls;
using System.Collections.Generic;
using System.Collections;
using System.Data.SqlClient;
using wbsbom_mysql;
using MySql.Data.MySqlClient;
public partial class WBSTree_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
//根节点是树控件的第一个节点 没有任何意义 只是显示作用
tree1.Nodes.Clear();
RadTreeNode rootNode = new RadTreeNode();
rootNode.Text = "";
rootNode.Value = "根节点";
rootNode.ImageUrl = "Img/mailfolder.gif";
rootNode.Expanded = true;
rootNode.ContextMenuName = "File";
//添加 顶层 的 “部门” 到 “根节点” 下
department_info[] _department_info = department_info.FindAllByProperty("FatherDepartment_info", null);
if (_department_info.Length == 0) {
RadAjaxManager1.Alert("暂时没有数据"); //跳出提示
return;
}
// ”部门“ 添加到 “根节点” 下
for (int i = 0; i < _department_info.Length; i++)
{
AddChildToTree(rootNode, _department_info[i]);
}
//子部门 添加到 父部门下
for (int i = 0; i < _department_info.Length; i++)
{
AddChildToTreeRecursion(rootNode.Nodes[i], _department_info[i]);
}
//全部添加到控件上
tree1.Nodes.Add(rootNode);
tree1.ExpandAllNodes();
}
}
节点添加到控件
/// <summary>
/// 处理节点移动
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.WebControls.AjaxRequestEventArgs"/> instance containing the event data.</param>
public void AjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
/*
客户端ajax request 代码
window["<%= RadAjaxManager1.ClientID %>"].AjaxRequest(sourceNode.TreeView.ClientID + "\r\n" + sourceNode.Text + "\r\n" + destNode.Text);
*/
string[] args = e.Argument.Replace("\r\n", "\n").Split('\n');
RadTreeView srcTree = null;
RadTreeView dstTree = null;
if (tree1.ClientID.Equals(args[0])) //这里tree1既是源也是目标
{
srcTree = tree1;
}
if (tree1.ClientID.Equals(args[0]))
{
dstTree = tree1;
}
RadTreeNode sourceNode = srcTree.FindNodeByText(args[1]);
RadTreeNode destNode = dstTree.FindNodeByText(args[2]);
RadTreeNode tempNode = destNode;
while (tempNode != null)
{
if (!tempNode.Equals(sourceNode))
{
tempNode = tempNode.Parent;
}
else
break;
}
if (tempNode != null)
{
RadAjaxManager1.Alert("父节点不可移动到子节点");
return;
}
RadTreeNode newNode = new RadTreeNode(sourceNode.Text, sourceNode.Value);
newNode.ImageUrl = "Img/mailfolder.gif";
newNode.Expanded = true;
AddChilds(newNode, sourceNode);
destNode.Nodes.Add(newNode);
destNode.ExpandChildNodes();
if (!sourceNode.Equals(srcTree.Nodes[0]))
{
sourceNode.Remove();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
HiberarchyOrder(tree1.Nodes[0]);
}
/// <summary>
/// 层次遍历该死的radcontrol,显示其节点信息给Label1,并保存结果至数据库
/// </summary>
/// <param name="rootNode"></param>
public void HiberarchyOrder(RadTreeNode rootNode)
{
List<RadTreeNode> Nodes = new List<RadTreeNode>();
//层次遍历
int front = -1;
int rear = -1;
if (rootNode != null)
{
rear++;
Nodes.Add(rootNode);
}
while (front != rear)
{
front++;
if (Nodes[front].Value == "根节点")
{
Nodes[front].ToolTip = "0";
}
rootNode = Nodes[front];
Label1.Text += "<p>" + rootNode.Value + " " + rootNode.Text + " 节点编码: " + rootNode.ToolTip + "</p>";
//节点编码
for (int i = 0; i < rootNode.Nodes.Count; i++)
{
if (rootNode.Nodes[i] != null)
{
rear++;
if (rootNode.Value == "根节点")//根节点不需要加" ."
{
rootNode.Nodes[i].ToolTip = (i + 1).ToString();
}
else
{
rootNode.Nodes[i].ToolTip = rootNode.ToolTip + "." + (i + 1).ToString();
}
Nodes.Add(rootNode.Nodes[i]);
}
}
string nodeFrontFatherValue = "";
//如果是根节点 或者 其层级为1层的 那么他们的上级就为null
if (Nodes[front].ToolTip == "0" || Get_department_info_LevelByWbs(Nodes[front].ToolTip) == 1)
{
nodeFrontFatherValue = "";
}
else
{
nodeFrontFatherValue = Nodes[front].Parent.Value;
}
int _department_info_Sn = -1;
if (Nodes[front].ToolTip != "0")
{
_department_info_Sn = Convert.ToInt32(Nodes[front].Value);
}
string WBS = Nodes[front].ToolTip;
int _department_info_Level = Get_department_info_LevelByWbs(Nodes[front].ToolTip);
string _Department_UpperSn = "";
//如果是根节点 或者 其层级为1层的 那么他们的上级就为null
if (Nodes[front].ToolTip == "0" || Get_department_info_LevelByWbs(Nodes[front].ToolTip) == 1)
{
_Department_UpperSn = "";
}
else
{
_Department_UpperSn = Nodes[front].Parent.Value;
}
//保存吧
if (_department_info_Sn != -1)
{
department_info _department_info = department_info.Find(_department_info_Sn);
_department_info.Department_WBS = WBS;
_department_info.Department_Level = _department_info_Level;
if (!string.IsNullOrEmpty(_Department_UpperSn))
{
_department_info.FatherDepartment_info = department_info.Find(Convert.ToInt32(_Department_UpperSn));
}
_department_info.Save();
}
}
}
/// <summary>
/// 若输入2.1 则 level为2 ,输入3.2.2 ,则level为 3
/// </summary>
/// <param name="wbsCode">The WBS code.</param>
/// <returns></returns>
public int Get_department_info_LevelByWbs(string wbsCode)
{
if (wbsCode == "0")
{
return 0;
}
int level = 0;
foreach (char c in wbsCode)
{
if (c == '.')
{
level++;
}
}
return level+1;
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.WebControls;
using System.Collections.Generic;
using System.Collections;
using System.Data.SqlClient;
using wbsbom_mysql;
using MySql.Data.MySqlClient;
public partial class WBSTree_Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
//根节点是树控件的第一个节点 没有任何意义 只是显示作用
tree1.Nodes.Clear();
RadTreeNode rootNode = new RadTreeNode();
rootNode.Text = "";
rootNode.Value = "根节点";
rootNode.ImageUrl = "Img/mailfolder.gif";
rootNode.Expanded = true;
rootNode.ContextMenuName = "File";
//添加 顶层 的 “部门” 到 “根节点” 下
department_info[] _department_info = department_info.FindAllByProperty("FatherDepartment_info", null);
if (_department_info.Length == 0) {
RadAjaxManager1.Alert("暂时没有数据"); //跳出提示
return;
}
// ”部门“ 添加到 “根节点” 下
for (int i = 0; i < _department_info.Length; i++)
{
AddChildToTree(rootNode, _department_info[i]);
}
//子部门 添加到 父部门下
for (int i = 0; i < _department_info.Length; i++)
{
AddChildToTreeRecursion(rootNode.Nodes[i], _department_info[i]);
}
//全部添加到控件上
tree1.Nodes.Add(rootNode);
tree1.ExpandAllNodes();
}
}
节点添加到控件
/// <summary>
/// 处理节点移动
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Telerik.WebControls.AjaxRequestEventArgs"/> instance containing the event data.</param>
public void AjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
{
/*
客户端ajax request 代码
window["<%= RadAjaxManager1.ClientID %>"].AjaxRequest(sourceNode.TreeView.ClientID + "\r\n" + sourceNode.Text + "\r\n" + destNode.Text);
*/
string[] args = e.Argument.Replace("\r\n", "\n").Split('\n');
RadTreeView srcTree = null;
RadTreeView dstTree = null;
if (tree1.ClientID.Equals(args[0])) //这里tree1既是源也是目标
{
srcTree = tree1;
}
if (tree1.ClientID.Equals(args[0]))
{
dstTree = tree1;
}
RadTreeNode sourceNode = srcTree.FindNodeByText(args[1]);
RadTreeNode destNode = dstTree.FindNodeByText(args[2]);
RadTreeNode tempNode = destNode;
while (tempNode != null)
{
if (!tempNode.Equals(sourceNode))
{
tempNode = tempNode.Parent;
}
else
break;
}
if (tempNode != null)
{
RadAjaxManager1.Alert("父节点不可移动到子节点");
return;
}
RadTreeNode newNode = new RadTreeNode(sourceNode.Text, sourceNode.Value);
newNode.ImageUrl = "Img/mailfolder.gif";
newNode.Expanded = true;
AddChilds(newNode, sourceNode);
destNode.Nodes.Add(newNode);
destNode.ExpandChildNodes();
if (!sourceNode.Equals(srcTree.Nodes[0]))
{
sourceNode.Remove();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
HiberarchyOrder(tree1.Nodes[0]);
}
/// <summary>
/// 层次遍历该死的radcontrol,显示其节点信息给Label1,并保存结果至数据库
/// </summary>
/// <param name="rootNode"></param>
public void HiberarchyOrder(RadTreeNode rootNode)
{
List<RadTreeNode> Nodes = new List<RadTreeNode>();
//层次遍历
int front = -1;
int rear = -1;
if (rootNode != null)
{
rear++;
Nodes.Add(rootNode);
}
while (front != rear)
{
front++;
if (Nodes[front].Value == "根节点")
{
Nodes[front].ToolTip = "0";
}
rootNode = Nodes[front];
Label1.Text += "<p>" + rootNode.Value + " " + rootNode.Text + " 节点编码: " + rootNode.ToolTip + "</p>";
//节点编码
for (int i = 0; i < rootNode.Nodes.Count; i++)
{
if (rootNode.Nodes[i] != null)
{
rear++;
if (rootNode.Value == "根节点")//根节点不需要加" ."
{
rootNode.Nodes[i].ToolTip = (i + 1).ToString();
}
else
{
rootNode.Nodes[i].ToolTip = rootNode.ToolTip + "." + (i + 1).ToString();
}
Nodes.Add(rootNode.Nodes[i]);
}
}
string nodeFrontFatherValue = "";
//如果是根节点 或者 其层级为1层的 那么他们的上级就为null
if (Nodes[front].ToolTip == "0" || Get_department_info_LevelByWbs(Nodes[front].ToolTip) == 1)
{
nodeFrontFatherValue = "";
}
else
{
nodeFrontFatherValue = Nodes[front].Parent.Value;
}
int _department_info_Sn = -1;
if (Nodes[front].ToolTip != "0")
{
_department_info_Sn = Convert.ToInt32(Nodes[front].Value);
}
string WBS = Nodes[front].ToolTip;
int _department_info_Level = Get_department_info_LevelByWbs(Nodes[front].ToolTip);
string _Department_UpperSn = "";
//如果是根节点 或者 其层级为1层的 那么他们的上级就为null
if (Nodes[front].ToolTip == "0" || Get_department_info_LevelByWbs(Nodes[front].ToolTip) == 1)
{
_Department_UpperSn = "";
}
else
{
_Department_UpperSn = Nodes[front].Parent.Value;
}
//保存吧
if (_department_info_Sn != -1)
{
department_info _department_info = department_info.Find(_department_info_Sn);
_department_info.Department_WBS = WBS;
_department_info.Department_Level = _department_info_Level;
if (!string.IsNullOrEmpty(_Department_UpperSn))
{
_department_info.FatherDepartment_info = department_info.Find(Convert.ToInt32(_Department_UpperSn));
}
_department_info.Save();
}
}
}
/// <summary>
/// 若输入2.1 则 level为2 ,输入3.2.2 ,则level为 3
/// </summary>
/// <param name="wbsCode">The WBS code.</param>
/// <returns></returns>
public int Get_department_info_LevelByWbs(string wbsCode)
{
if (wbsCode == "0")
{
return 0;
}
int level = 0;
foreach (char c in wbsCode)
{
if (c == '.')
{
level++;
}
}
return level+1;
}
}
Future Reading:Rails生成ExtTee