剛我在做Tree view 綁定時自己摸索了一下,網(wǎng)上有人說TreeView綁定數(shù)據(jù)源,用什么遞歸綁定啥的,我不想看了,就自己試著寫了一個
我是這樣做的,如果有什么問題請大神指導(dǎo),我是菜鳥額..
1:頁面TreeView 叫tvFunction
2:新建 adminFunction.xml
/// summary>
/// 初始化功能樹結(jié)構(gòu)
/// /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)
{
//獲取父節(jié)點的信息顯示(輕狂-書生)
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);
}
}