前台代码
<body style="background-color: #B1E7FF;">
<form id="form1" runat="server">
<div style="width: 180px; height: 100%">
<asp:TreeView ID="TV_FuncList" runat="server" Width="186px" ShowLines="True">
<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
<Nodes>
</Nodes>
<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="0px"
NodeSpacing="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
</div>
</form>
</body>
<script type="text/javascript">
function oclick(obj) {
if (document.getElementById("hover") != null) document.getElementById("hover").removeAttribute("id");
obj.setAttribute("id", "hover");
}
</script>
</html>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string filePath = AppDomain.CurrentDomain.BaseDirectory + "MenuTree/MenuTree01.xml";
if (!string.IsNullOrEmpty(Request.QueryString["id"]))
{
if (Request.QueryString["id"].ToString() == "2")
{
filePath = AppDomain.CurrentDomain.BaseDirectory + "MenuTree/MenuTree02.xml";
}
}
BindTreeView(TV_FuncList, filePath);
}
}
/// <summary>
/// 绑定数据到树型控件上
/// </summary>
/// <param name="tv">树型控件对象</param>
/// <param name="filePath">xml文件的路径</param>
private void BindTreeView(TreeView tv, string filePath)
{
tv.Nodes.Clear();
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
{
return;
}
XElement rootNode = XElement.Load(filePath);
if (rootNode.Attribute("level") != null && rootNode.Attribute("level").Value != "-1")
{
tv.ExpandDepth = int.Parse(rootNode.Attribute("level").Value.Trim());
}
IEnumerable<XElement> nodeList = rootNode.Elements();
foreach (XElement node in nodeList)
{
TreeNode treeNode = new TreeNode();
treeNode.Text = node.Attribute("name").Value.Trim();
treeNode.Value = node.Attribute("guid").Value.Trim();
treeNode.ToolTip = node.Attribute("name").Value.Trim();
if ((node.Attribute("icon") != null) && (node.Attribute("icon").Value.Trim() != ""))
treeNode.ImageUrl = node.Attribute("icon").Value.ToString();
//if (node.HasElements)
//{
treeNode.SelectAction = TreeNodeSelectAction.Expand;
//处理子节点
AddChildTreeNode(ref treeNode, node, node.Attribute("name").Value.Trim());
//}
//else
//{
if (node.Attribute("code") != null)
{
treeNode.NavigateUrl = string.Format("javascript:parent.goToUrl('{0}','{1}');", node.Attribute("code").Value.Trim(), node.Attribute("name").Value.Trim());
}
//}
tv.Nodes.Add(treeNode);
}
}
/// <summary>
/// 给树型控件的根节点添加子节点
/// </summary>
/// <param name="treeNode">树型节点</param>
/// <param name="xmlNode">Xml元素节点</param>
/// <param name="parentNodeName">上一节点的名称</param>
private void AddChildTreeNode(ref TreeNode treeNode, XElement xmlNode, string parentNodeName)
{
IEnumerable<XElement> nodeList = xmlNode.Elements();
if (nodeList != null)
{
foreach (XElement node in nodeList)
{
TreeNode childTreeNode = new TreeNode();
childTreeNode.Text = node.Attribute("name").Value.Trim();
childTreeNode.Value = node.Attribute("guid").Value.Trim();
childTreeNode.ToolTip = node.Attribute("name").Value.Trim();
if ((node.Attribute("icon") != null) && (node.Attribute("icon").Value.Trim() != ""))
childTreeNode.ImageUrl = node.Attribute("icon").Value.ToString();
//if (node.HasElements)
//{
childTreeNode.SelectAction = TreeNodeSelectAction.Expand;
AddChildTreeNode(ref childTreeNode, node, node.Attribute("name").Value.Trim());
//}
//else
//{
if (node.Attribute("code") != null)
{
childTreeNode.NavigateUrl = string.Format("javascript:parent.goToUrl('{0}','{1}');", node.Attribute("code").Value.Trim(), node.Attribute ("name").Value.Trim());
} //} treeNode.ChildNodes.Add(childTreeNode); } } }
} }