tree递归算法

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Student.Models;
using Student.BLL;
using System.Collections.Generic;
public partial class SysFunTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginInfo user = Session["CurrentUser"] as LoginInfo;
        if (!Page.IsPostBack)
        {
        

            DisplayUserMenu(user);
         

        }
    }
    protected void DisplayUserMenu(LoginInfo user)
    {
        tvUserRightMenu.Nodes.Clear();

        IList<SysFun> parentSysFun = SysFunManager.GetAllByTiaojian(user.URole.RoleId);
        if (parentSysFun.Count != 0)
        {
            foreach (SysFun sfParent in parentSysFun)
            {

                string nodeId = sfParent.NodeId.ToString();//第一层节点id
                string displayName = sfParent.DisplayName;//
第一层节点显示名称

                TreeNode fatherNode = this.CreatTreeNode(displayName, nodeId, "", "Image/fclose.gif");//根据节点信息,创建第一层节点

                CreateChildTree(sfParent.NodeId.ToString(), user.URole.RoleId, fatherNode);//创建子节点

                tvUserRightMenu.Nodes.Add(fatherNode);//将第一层节点加入到用户权限TreeView

            }
        }
        else
        {
            Response.Write("<script>alert('
您没有这些权限!
')</script>");
        }
     
    }

    //创建第二层节点
    private void CreateChildTree(string nodeId, int roleId, TreeNode fatherNode)
    {
        IList<SysFun> childSysFun = SysFunManager.GetAllZiByFu(Convert.ToInt32(nodeId), roleId);//
获得父节点为nodeId的所有子节点

        foreach (SysFun sfChild in childSysFun)
        {
            string childNodeId = sfChild.NodeId.ToString();//
第二层节点
id
            string childDisplayName = sfChild.DisplayName;//
第二层节点名称

            string nodeURL = ResolveUrl(sfChild.NodeURL.Trim());//
将路径转换为在客户端可用的URL

            TreeNode childNode = this.CreatTreeNode(childDisplayName, childNodeId, nodeURL, "Image/doc.gif");//根据节点信息,创建第二层节点
            AddTree(fatherNode, childNode);//
将子节点加入到父节点中
        }
    }


    /// <summary>
    ///
创建一个树节点,返回一个树节点对象,参数内容是:
    ///
节点名称,节点ID,链接地址,正常图标,展开后的图标
    /// </summary>
    private TreeNode CreatTreeNode(string strText, string strId, string strUrl, string strImg)
    {
        TreeNode newNode = new TreeNode();
        newNode.Text = strText;
        newNode.Value = strId;
        newNode.NavigateUrl = strUrl;
        newNode.ImageUrl = strImg;
        return newNode;
    }

    /// <summary>
    ///
把子节点添加到父节点当中

    /// </summary>
    private void AddTree(TreeNode FatherNode, TreeNode ChildNode)
    {
        FatherNode.ChildNodes.Add(ChildNode);
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值