前台:
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:treeview runat="server" ID="tvMenu" ImageSet="Contacts"
onselectednodechanged="tvMenu_SelectedNodeChanged" Width="266px" NodeIndent="10">
<HoverNodeStyle Font-Underline="False" />
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="True" ForeColor="#5555DD" />
<SelectedNodeStyle Font-Underline="True"
HorizontalPadding="0px" VerticalPadding="0px" />
</asp:treeview>
</div>
</form>
</body>
</html>
后台:
public partial class menu : System.Web.UI.Page
{
private SqlConnection conn;
private SqlCommand cmd;
private const string connStr = "Server=115.29.244.185,9838;initial catalog=DbDevice;User=prkang_v2_access;Pwd=password01!";
private string type = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
loadDefault();
}
void loadDefault()
{
//加载目录树
DataTable dtable0 = GetData0();
DataTable dtable = GetData1();
DataTable dtable2 = GetData2();
CreateTree(this.tvMenu,dtable0, dtable, dtable2);
}
protected void tvMenu_SelectedNodeChanged(object sender, EventArgs e)
{
Session["MenuId"] = string.Empty;
if (this.tvMenu.SelectedNode.Value != null)
{
Session["MenuId"] = this.tvMenu.SelectedValue;
}
Response.Write("<script>window.close();window.opener.reflash();window.opener=null;</script>");
//前我做能脚本某写错需要测试
}
private DataTable GetData0()
{
DataTable datatable = GetTable("select * from PB_STAFF");
return datatable;
}
private DataTable GetData1()
{
DataTable dt = GetTable("select * from Tbl_Dealer");
return dt;
}
private DataTable GetData2()
{
DataTable dt = GetTable("select * from Tbl_Terminal");
return dt;
}
#region 创建树、节点
/// <summary>
/// 创建树
/// </summary>
/// <param name="treeView"></param>
/// <param name="treeData"></param>
private void CreateTree(TreeView treeView,DataTable pbData, DataTable treeData, DataTable subdata)
{
try
{
var ss = SessionUtil.GetStaffSession();
var model = PRKEP.Data.PB_STAFF.SingleOrDefault(x => x.PB_LOGIN_ID == ss.LoginId);
string dname = model.PB_NAME;
string DealerId = model.PB_LOGIN_ID;
treeView.Nodes.Clear();//清空树所节点
treeView.ExpandDepth = 10;
TreeNode rootNode = new TreeNode();//创建根节点
rootNode.Text = dname;
rootNode.Value = DealerId;
rootNode.ImageUrl = "../../Images/icon_remind.gif";
treeView.Nodes.Add(rootNode);//添加根节点
CreateChildNode(rootNode,pbData,treeData, subdata);//创建其节点
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "fault", "alert(‘" + ex.Message.Replace("\n", "").Replace("\r", "") + "‘);", true);
}
}
#endregion
#region 创建销售节点
/// <summary>
/// 创建销售节点
/// </summary>
/// <param name="parentNode"></param>
/// <param name="treeData"></param>
/// <param name="subData"></param>
private static void CreateChildNode(TreeNode parentNode,DataTable pbData ,DataTable treeData, DataTable subData)
{
try
{
DataRow[] rowList = pbData.Select("PB_PARENT_ID=‘" + parentNode.Value + "‘");
foreach (DataRow row in rowList)
{
TreeNode node = new TreeNode();//创建新节点
node.Text = row["PB_NAME"].ToString();
node.Value = row["PB_LOGIN_ID"].ToString();
parentNode.ChildNodes.Add(node);
CreateChildNode(node, pbData, treeData, subData);//递归调用创建其销售员节点
string exp2 = "ParentId=‘"+node.Value+"‘";
DataRow[] rowlist2 = treeData.Select(exp2);
if (rowlist2 != null)
{
foreach (DataRow rows in rowlist2)
{
TreeNode node1 = new TreeNode();//创建新节点
node1.Value = rows["DealerId"].ToString();
node1.Text = rows["DealerName"].ToString();
parentNode.ChildNodes.Add(node1);
CreatexiaoshouChildNode(node1, treeData);//递归调用创建其经销商节点
string exp3 = "DealerId=‘" + node1.Value + "‘";
DataRow[] rowlist3 = subData.Select(exp3);
if (rowlist3 != null)
{
foreach (DataRow rowlist in rowlist3)
{
TreeNode node2 = new TreeNode();
node2.Value = rows["TerminalId"].ToString();
node2.Text = rows["TerminalName"].ToString();
parentNode.ChildNodes.Add(node2);
CreateSubChildNode(node2, subData);
}
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 创建销售人员下属的销售人员
/// <summary>
/// 创建销售人员下属的销售人员
/// </summary>
/// <param name="parentNode"></param>
/// <param name="subtreeData"></param>
private static void CreatexiaoshouChildNode(TreeNode parentNode, DataTable treeData)
{
try
{
DataRow[] rowlist2 = treeData.Select("ParentId=‘" + parentNode.Value + "‘");
if (rowlist2 != null)
{
foreach (DataRow row1 in rowlist2)
{
TreeNode node1 = new TreeNode();//创建新节点
node1.Text = row1["DealerName"].ToString();
node1.Value = row1["DealerId"].ToString();
parentNode.ChildNodes.Add(node1);
CreatexiaoshouChildNode(node1, treeData);//递归调用创建其节点
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 创建经销商下属的终端客户节点
/// <summary>
/// 创建经销商下属的终端客户节点
/// </summary>
/// <param name="parentNode"></param>
/// <param name="subtreeData"></param>
private static void CreateSubChildNode(TreeNode parentNode, DataTable subtreeData)
{
try
{
DataRow[] rowlist2 = subtreeData.Select("DealerId=‘" + parentNode.Value + "‘");
if (rowlist2 != null)
{
foreach (DataRow row1 in rowlist2)
{
TreeNode node1 = new TreeNode();//创建新节点
node1.Text = row1["TerminalName"].ToString();
node1.Value = row1["TerminalId"].ToString();
parentNode.ChildNodes.Add(node1);
CreateSubChildNode(node1, subtreeData);//递归调用创建其节点
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取经销商对应的级别名称
/// <summary>
/// 获取经销商对应的级别名称
/// </summary>
/// <param name="level"></param>
/// <returns></returns>
private static string getDealerLevelName(string level)
{
string str = string.Empty;
switch (level)
{
case "0":
str = "钻石";
break;
case "1":
str = "金牌";
break;
case "2":
str = "银牌";
break;
default:
str = "铜牌";
break;
}
return str;
}
#endregion
#region 数据库操作
/// <summary>
/// 打链接
/// </summary>
///<returns></returns>
private SqlConnection GetConn()
{
if (conn == null)
conn = new SqlConnection(connStr);
if (conn.State == ConnectionState.Closed)
conn.Open();
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
} return conn;
}
/// <summary>
/// 执行增删改查操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private int ExecuteNonQuery(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteNonQuery();
}
catch
{
return 0;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 读数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private SqlDataReader ExecuteReader(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteReader();
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 该表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private DataTable GetTable(string sql)
{
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, GetConn());
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
#endregion
}