前台:
选择目录树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
}