62,254
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
namespace WeiYu.Model
{
/// <summary>
/// 菜单节点类
/// </summary>
[Serializable]
public class SysFun
{
private int nodeId;//菜单节点id
private string displayName;//菜单名称
private string nodeURL;//菜单连接地址
private int displayOrder;//菜单显示顺序
private int parentNodeId;//父节点id
public int NodeId
{
get { return nodeId; }
set { nodeId = value; }
}
public string DisplayName
{
get { return displayName; }
set { displayName = value; }
}
public string NodeURL
{
get { return nodeURL; }
set { nodeURL = value; }
}
public int DisplayOrder
{
get { return displayOrder; }
set { displayOrder = value; }
}
public int ParentNodeId
{
get { return parentNodeId; }
set { parentNodeId = value; }
}
}
}
<div id="divMenu" runat="server" align="center" style="width: 160px; height:385px; background-color: #6dc7fc">
<div style="width: 120px; height: 20px"></div>
<div style="width: 140px;">
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TreeView ID="TVSysFun" runat="server" ExpandDepth="0" align="left"></asp:TreeView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
public void DisplayUserMenu(UserInfo user)
{
this.TVSysFun.Nodes.Clear();
//得到系统菜单表中所有的第一级菜单
IList<SysFun> parentSysFun = SysFunManager.GetAllParentNodeInfoByUserId(user);
foreach (SysFun sfParent in parentSysFun)
{
//得到第一层节点的Id
string nodeId = sfParent.NodeId.ToString();
//得到第一层节点的显示名称
string displayName = sfParent.DisplayName;
//根据节点信息创建一层节点
TreeNode fatherNode = CreateTreeNode(displayName, nodeId, "", "~/images/menuclose.gif");
CreateChildTree(nodeId, user, fatherNode);
this.TVSysFun.Nodes.Add(fatherNode);
//选中的子菜单其父节点打开
}
if (Request.QueryString["NodeId"] != null)
{
string NodeId = Request.QueryString["NodeId"];
//选中的子菜单其父节点打开
if (NodeId != "" && NodeId.Length > 3)
{
foreach (TreeNode treeNode in this.TVSysFun.Nodes)
{
if (NodeId.IndexOf(treeNode.Value.ToString()) == 0)
{
treeNode.Expand();
}
}
}
}
}
private TreeNode CreateTreeNode(string strText, string strId, string strUrl, string strImage)
{
TreeNode newNode = new TreeNode();
newNode.Text = strText;
newNode.Value = strId;
if (strId != "")
{
newNode.NavigateUrl = strUrl + "?NodeId=" + strId;
}
else
{
newNode.NavigateUrl = strUrl;
}
newNode.ImageUrl = strImage;
return newNode;
}
//创建第二级节点
public void CreateChildTree(string nodeId, UserInfo user, TreeNode fatherNode)
{
//在三层下实现获得父级节点为nodeId的所有子节点
IList<SysFun> childSysFun = SysFunManager.GetSysFunByParentNodeIdAndUserId(user, int.Parse(nodeId));
foreach (SysFun sfChild in childSysFun)
{
//得到第二层节点Id
string childNodeId = sfChild.NodeId.ToString();
//得到第二层节点的显示名称
string childDisplayName = sfChild.DisplayName;
//将路径转换为客户端可用的URL
string nodeURL = ResolveUrl(sfChild.NodeURL.ToString().Trim());
//根据节点信息,创建第二层节点
TreeNode childNode = CreateTreeNode(childDisplayName, childNodeId, nodeURL, "~/images/CloseTree.gif");
//将子节点加入到父节点中
AddTree(fatherNode, childNode);
}
}
//将子节点加入到父节点中
private void AddTree(TreeNode fatherNode, TreeNode childNode)
{
fatherNode.ChildNodes.Add(childNode);
}