从数据库读取数据绑定到TreeView(内含设置样式,图片)
/// <summary>
/// 邦定根节点
/// </summary>
public void BindTreeView(DataTable dt)
{
DataTable aldt = GetTreeList("").Tables[0];
//菜单状态
tv.Nodes.Clear(); // 清空树
if (Request.QueryString["Type"] == "User")
{
TreeNode rootnode = new TreeNode();
rootnode.Text = "--所有部门--";
rootnode.Value = "";
rootnode.ImageUrl = "../img/0_Person.gif";
rootnode.NavigateUrl = "../User/AdminUser.aspx";
rootnode.Target = "mainFrame";
tv.Nodes.Add(rootnode);
}
foreach (DataRow r in dt.Rows)
{
string nodeid = r["dept_code"].ToString();
string text = r["dept_name"].ToString();
this.tv.Font.Name = "宋体";
this.tv.Font.Size = FontUnit.Parse("9");
//权限控制菜单
TreeNode rootnode = new TreeNode();
rootnode.Text = text;
rootnode.Value = nodeid;
rootnode.ImageUrl = "../img/0_Person.gif";
if (Request.QueryString["Type"] == "User")
{
rootnode.NavigateUrl = "../User/AdminUser.aspx?dept_code=" + nodeid;
}
else
{
rootnode.NavigateUrl = "DeptRight.aspx?dept_code=" + nodeid;
}
rootnode.Target = "mainFrame";
tv.Nodes.Add(rootnode);
string sonparentid = nodeid;
CreateNode(sonparentid, rootnode, aldt);
}
}
/// <summary>
/// 邦定任意节点
/// </summary>
public void CreateNode(string parentid, TreeNode parentnode, DataTable dt)
{
DataRow[] drs = dt.Select("pare_code= '" + parentid + "'");//选出所有子节点
foreach (DataRow r in drs)
{
string nodeid = r["dept_code"].ToString();
string text = r["dept_name"].ToString();
//treeview set
this.tv.Font.Name = "宋体";
this.tv.Font.Size = FontUnit.Parse("9");
TreeNode node = new TreeNode();
node.Text = text;
node.Value = nodeid;
node.ImageUrl = "../img/0_Person.gif";
if (Request.QueryString["Type"] == "User")
{
node.NavigateUrl = "../User/AdminUser.aspx?dept_code=" + nodeid;
}
else
{
node.NavigateUrl = "DeptRight.aspx?dept_code=" + nodeid;
}
node.Target = "mainFrame";
//node.Expanded=true;
//node.SelectAction = TreeNodeSelectAction.Expand; //和ASP.NET1.1中TREEVIEW的SelectExpands属性等效
string sonparentid = nodeid;// or =location
if (parentnode == null)
{
tv.Nodes.Clear();
parentnode = new TreeNode();
tv.Nodes.Add(parentnode);
}
parentnode.ChildNodes.Add(node);
CreateNode(sonparentid, node, dt);
}
}