1。
数据来自数据库迈?2。
递归动态创建 TreeView 节点<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %><%--http://community.csdn.net/Expert/TopicView3.asp?id=5601936--%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
CreateCityTree();
}
} void CreateCityTree()
{
DataTable dtCity = CreateCityTable();
CreateCityTreeRecursive(dtCity, treeCity, null, 0);
} void CreateCityTreeRecursive(DataTable soure, TreeView tree, TreeNode parentNode, int parentId)
{
DataRow[] rows = soure.Select("ParentID=" + parentId.ToString());
foreach(DataRow r in rows){
TreeNode node = new TreeNode();
node.Text = r["CityName"].ToString();
node.NavigateUrl = "city.aspx?cityid=" + r["CityID"].ToString();
if (tree != null) {
tree.Nodes.Add(node);
}
else {
parentNode.ChildNodes.Add(node);
}
CreateCityTreeRecursive(soure, null, node, (int)r["CityID"]);
}
} #region sample data
public static DataTable CreateCityTable()
{
DataTable tbl = new DataTable();
tbl.TableName = "CityTable";
tbl.Columns.Add("CityID", typeof(int));
tbl.Columns.Add("CityName", typeof(string));
tbl.Columns.Add("ParentID", typeof(int));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Chongqing";
row[2] = 0;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 2;
row[1] = "Shapingba";
row[2] = 1;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 3;
row[1] = "Yuzhongqu";
row[2] = 1;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 4;
row[1] = "Sichuan";
row[2] = 0;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 5;
row[1] = "Chengdu";
row[2] = 4;
tbl.Rows.Add(row); return tbl;
} #endregion
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="treeCity" runat="server"></asp:TreeView>
</div>
</form>
</body>
</html>
数据来自数据库迈?2。
递归动态创建 TreeView 节点<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %><%--http://community.csdn.net/Expert/TopicView3.asp?id=5601936--%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">
void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
CreateCityTree();
}
} void CreateCityTree()
{
DataTable dtCity = CreateCityTable();
CreateCityTreeRecursive(dtCity, treeCity, null, 0);
} void CreateCityTreeRecursive(DataTable soure, TreeView tree, TreeNode parentNode, int parentId)
{
DataRow[] rows = soure.Select("ParentID=" + parentId.ToString());
foreach(DataRow r in rows){
TreeNode node = new TreeNode();
node.Text = r["CityName"].ToString();
node.NavigateUrl = "city.aspx?cityid=" + r["CityID"].ToString();
if (tree != null) {
tree.Nodes.Add(node);
}
else {
parentNode.ChildNodes.Add(node);
}
CreateCityTreeRecursive(soure, null, node, (int)r["CityID"]);
}
} #region sample data
public static DataTable CreateCityTable()
{
DataTable tbl = new DataTable();
tbl.TableName = "CityTable";
tbl.Columns.Add("CityID", typeof(int));
tbl.Columns.Add("CityName", typeof(string));
tbl.Columns.Add("ParentID", typeof(int));
DataRow row = tbl.NewRow();
row[0] = 1;
row[1] = "Chongqing";
row[2] = 0;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 2;
row[1] = "Shapingba";
row[2] = 1;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 3;
row[1] = "Yuzhongqu";
row[2] = 1;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 4;
row[1] = "Sichuan";
row[2] = 0;
tbl.Rows.Add(row); row = tbl.NewRow();
row[0] = 5;
row[1] = "Chengdu";
row[2] = 4;
tbl.Rows.Add(row); return tbl;
} #endregion
</script><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="treeCity" runat="server"></asp:TreeView>
</div>
</form>
</body>
</html>
如楼上说,也是一种!数据从那里来!
数据库SQL或者是XML的!
SQL根据权限查询数据再绑定。
网上baidu一下有很多这方面权限的问题!
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;using BC.Engine.Steed.IDAL;
using BC.Engine.Steed.DALFactory;
using BC.Engine.Steed.Model.PlatForm;
namespace BC.Engine.Steed.BLL.Controls
{ public enum MenuType
{
AllMenu,
NormalMenu
}; public class SteedTreeView : System.Web.UI.WebControls.TreeView
{
private IFunctionList dal;
private FunctionInfoCollection functionInfoCollection; private MenuType functionMenuType;
public MenuType FunctionMenuType
{
get
{
return functionMenuType;
}
set
{
functionMenuType = value;
}
} public string CurrentUrl
{
get
{
if (ViewState["CurrentUrl"] == null)
return "";
else
return ViewState["CurrentUrl"].ToString();
}
set
{
ViewState["CurrentUrl"] = value;
}
} public string UserId
{
get
{
if (ViewState["UserId"] == null)
return "0";
else
return ViewState["UserId"].ToString();
}
set
{
ViewState["UserId"] = value;
}
} public FunctionInfo CurrentFunctionInfo
{
get
{
dal = DataAccess.CreateFunctionList();
return dal.GetCurrentFunctionInfo(CurrentUrl);
}
} public FunctionInfo RootFunctionInfo
{
get
{
dal = DataAccess.CreateFunctionList();
return dal.GetCurrentRootFunctionInfo(CurrentUrl);
}
} public string ViewOrder
{
get
{
if (ViewState["ViewOrder"] == null)
return "";
else
return ViewState["ViewOrder"].ToString();
}
set
{
ViewState["ViewOrder"] = value;
}
} public int Depth
{
get
{
if (ViewState["Depth"] == null)
return int.MaxValue;
else
return Convert.ToInt32(ViewState["Depth"]);
}
set
{
ViewState["Depth"] = value;
}
} public SteedTreeView()
: base()
{
this.FunctionMenuType = MenuType.AllMenu;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dal = DataAccess.CreateFunctionList();
this.CurrentUrl = HttpUtility.UrlDecode(this.Page.Request.Url.AbsolutePath);
} protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsPostBack)
{
DataBindTree();
}
}
public void DataBindTree()
{
this.Nodes.Clear();
if (this.functionMenuType == MenuType.AllMenu)
{
BindFullFunctions();
}
if (this.functionMenuType == MenuType.NormalMenu)
{
BindNormalFunctions();
}
} void BindNormalFunctions()
{
BFunctionList functionList = new BFunctionList();
functionInfoCollection = functionList.GetChildFuctionListByViewOrderExceptSelf(this.RootFunctionInfo.ViewOrder, this.UserId, Depth); for (int i = 0; i < functionInfoCollection.Count; i++)
{
if (this.RootFunctionInfo.ViewOrder.Length + 2 == functionInfoCollection[i].ViewOrder.Length && Depth >= functionInfoCollection[i].ViewOrder.Length)
{
TreeNode rootNote = new TreeNode(functionInfoCollection[i].Name,
functionInfoCollection[i].ID,
functionInfoCollection[i].FMBigIconFile);
rootNote.ToolTip = functionInfoCollection[i].ViewOrder; if (FunctionMenuType == MenuType.NormalMenu)
rootNote.NavigateUrl = functionInfoCollection[i].Url; this.Nodes.Add(rootNote);
BuildChildNode(rootNote);
}
}
} public void SetSelectedNote(TreeNode node, string noteValue)
{
foreach (TreeNode childnode in node.ChildNodes)
{
if (childnode.Value == noteValue)
{
childnode.Selected = true;
return;
}
else
{
SetSelectedNote(childnode, noteValue);
}
}
} void BindFullFunctions()
{
functionInfoCollection = dal.GetFullFuctionList();
TreeNode rootNote = new TreeNode(functionInfoCollection[0].Name,
functionInfoCollection[0].ID,
functionInfoCollection[0].FMBigIconFile); rootNote.ToolTip = functionInfoCollection[0].ViewOrder;
this.Nodes.Add(rootNote);
BuildChildNode(rootNote);
} void BuildChildNode(TreeNode node)
{
for (int i = 0; i < functionInfoCollection.Count; i++)
{
FunctionInfo var = functionInfoCollection[i];
if (node.ToolTip.Length + 2 == var.ViewOrder.Length &&
node.ToolTip == var.ViewOrder.Substring(0, node.ToolTip.Length))
{
TreeNode chlNode = new TreeNode(var.Name, var.ID, var.FMBigIconFile);
chlNode.ToolTip = var.ViewOrder; if (FunctionMenuType == MenuType.NormalMenu)
chlNode.NavigateUrl = var.Url; node.ChildNodes.Add(chlNode);
BuildChildNode(chlNode);
}
} }
}}
functionInfoCollection = functionList.GetChildFuctionListByViewOrderExceptSelf(this.RootFunctionInfo.ViewOrder, this.UserId, Depth);这个方法,内部是返回 用户所有的 权限 菜单