summary目录层级_多级目录树,关联多个数据表

本文介绍如何在前端使用TreeView控件构建一个响应onselectednodechanged事件的多级目录树,并在后台通过SQL查询从PB_STAFF、Tbl_Dealer、Tbl_Terminal三个数据表获取数据,实现数据的关联加载。通过递归方法创建销售员、经销商及终端客户的子节点,并在选中节点时保存菜单ID到Session。
摘要由CSDN通过智能技术生成

前台:

选择目录树

onselectednodechanged="tvMenu_SelectedNodeChanged" Width="266px" NodeIndent="10">

HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />

HorizontalPadding="0px" VerticalPadding="0px" />

后台:

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("");

//前我做能脚本某写错需要测试

}

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  创建树、节点

///

/// 创建树

///

///

///

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 创建销售节点

///

/// 创建销售节点

///

///

///

///

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);//递归调用创建其经销商节点

}

}

}

}

catch (Exception ex)

{

throw ex;

}

}

#endregion

#region 创建销售人员下属的销售人员

///

/// 创建销售人员下属的销售人员

///

///

///

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);//递归调用创建其节点

string exp3 = "DealerId='" + node1.Value + "'";

DataRow[] rowlist3 = subData.Select(exp3);

if (rowlist3 != null && rowlist3.Count() > 0)

{

CreateSubChildNode(node1, subData);

}

}

}

}

catch (Exception ex)

{

throw ex;

}

}

#endregion

#region  创建经销商下属的终端客户节点

///

/// 创建经销商下属的终端客户节点

///

///

///

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  获取经销商对应的级别名称

///

/// 获取经销商对应的级别名称

///

///

///

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 数据库操作

///

/// 打链接

///

///

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;

}

///

/// 执行增删改查操作

///

///

///

private int ExecuteNonQuery(string sql)

{

try

{

cmd = new SqlCommand(sql, GetConn());

return cmd.ExecuteNonQuery();

}

catch

{

return 0;

}

finally

{

conn.Close();

}

}

///

/// 读数据

///

///

///

private SqlDataReader ExecuteReader(string sql)

{

try

{

cmd = new SqlCommand(sql, GetConn());

return cmd.ExecuteReader();

}

catch

{

return null;

}

finally

{

conn.Close();

}

}

///

/// 该表数据

///

///

///

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值