剛我在做Tree view 綁定時自己摸索了一下,網上有人說TreeView綁定數據源,用什麼遞歸綁定啥的,我不想看了,就自己試著寫了一個
我是這樣做的,如果有什麼問題請大神指導,我是菜鳥額..
1:頁面TreeView 叫tvFunction
2:新建 adminFunction.xml
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<functions>
<function name="商品發布" url="~/page/leaves/leave.aspx">
<subfunction name="添加商品" url="~/page/leaves/leave.aspx">
</subfunction>
<subfunction name="添加商品" url="~/page/leaves/leave.aspx">
</subfunction>
</function>
<function name="留言發布" url="~/page/leaves/leave.aspx">
<subfunction name="添加留言" url="~/page/leaves/leave.aspx">
</subfunction>
<subfunction name="修改留言" url="~/page/leaves/leave.aspx">
</subfunction>
</function>
</functions>
3:頁面綁定
復制代碼 代碼如下:
/// <summary>
/// 初始化功能樹結構
/// </summary>
private void InitFunction()
{
XmlDocument xmldoc = new XmlDocument();
string xmlPath = "~/adminFunction.xml";
xmlPath = Server.MapPath(xmlPath);
xmldoc.Load(xmlPath);
XmlNode root = xmldoc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
//獲取父節點的信息顯示(輕狂-書生)
XmlElement x1 = node as XmlElement;
string name = x1.GetAttribute("name");
string url = x1.GetAttribute("url");
TreeNode tn = new TreeNode();
tn.Text = name;
tn.NavigateUrl = url;
foreach (XmlNode subnode in x1.ChildNodes)
{
XmlElement subxmlEmt = subnode as XmlElement;
string subname = subxmlEmt.GetAttribute("name");
string suburl = subxmlEmt.GetAttribute("url");
TreeNode subtn = new TreeNode();
subtn.Text = subname;
subtn.NavigateUrl = suburl;
tn.ChildNodes.Add(subtn);
}
tvFunction.Nodes.Add(tn);
}
}
4:顯示效果