TreeVIew树形菜单

 

 /// <summary>
        
/// 绑定根节点
        
/// </summary>
        protected void BindParentTree()
        {
            //取得当前登录用户id
            int userId = Session["uid"]!=null?int.Parse(Session["uid"].ToString()):-1;
            
            List<Model.MenusInfo> list = new BLL.MenusInfo().GetModelList(" (MenuID in(select PreMenuID from MenusInfo where MenusInfo.MenuID in(select roleAuthority.MenuID from RoleAuthority roleAuthority where roleAuthority.RoleID in(select userRole.RoleID from UserRole userRole where userRole.UserID="+userId +"))))");
            for (int i = 0; i < list.Count; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = list[i].MenuName;
                tn.Value = list[i].MenuID.ToString();
                this.TVMenu.Nodes.Add(tn);
                tn.Expanded = false;
                //BindSecondTreeItems(tn, Convert.ToInt32(list[i].MenuID));
                BindTreeItems (tn, Convert.ToInt32(list[i].MenuID));
            }
        }
        ///// <summary>
        
///// 绑定二级目录菜单
        
///// </summary>
        
///// <param name="tn"></param>
        
///// <param name="menuid"></param>
        //protected void BindSecondTreeItems(TreeNode tn, int menuid)
        
//{
        
//    //取得当前登录用户id
        
//    int userId = int.Parse(Session["uid"].ToString());
        
//    List<Model.MenusInfo> list = new BLL.MenusInfo().GetModelList(" MenusInfo.MenuID in(select roleAuthority.MenuID from RoleAuthority roleAuthority where roleAuthority.RoleID in(select userRole.RoleID from UserRole userRole where userRole.UserID=" + userId + ")) and PreMenuID='" + menuid + "' and MenuInfo='事项办理'");
        
//     if (list.Count > 0)
        
//    {
        
//        TreeNode secondonetn = new TreeNode();
        
//        secondonetn.Text = "事项办理";
        
//        secondonetn.Value = "事项办理";
        
//        tn.ChildNodes.Add(secondonetn);
        
//        BindTreeItems(secondonetn, Convert.ToInt32(list[0].PreMenuID),1);
        
//    }
        
//     List<Model.MenusInfo> list1 = new BLL.MenusInfo().GetModelList(" MenusInfo.MenuID in(select roleAuthority.MenuID from RoleAuthority roleAuthority where roleAuthority.RoleID in(select userRole.RoleID from UserRole userRole where userRole.UserID=" + userId + ")) and  PreMenuID='" + menuid + "' and MenuInfo='台帐管理与统计分析'");
        
//    if (list1.Count > 0)
        
//    {
        
//        TreeNode secondtwotn = new TreeNode();
        
//        secondtwotn.Text = "台账管理与统计";
        
//        secondtwotn.Value = "台账管理与统计";
        
//        tn.ChildNodes.Add(secondtwotn);
        
//        BindTreeItems(secondtwotn, Convert.ToInt32(list1[0].PreMenuID),2);
        
//    }
        
//    if(list1 .Count <=0&&list .Count <=0)
        
//    {
        
//        BindTreeItems(tn, menuid,0);
        
//    }
        
//    tn.Expanded = false;
            
        
//}
        /// <summary>
        
/// 绑定子节点
        
/// </summary>
        
/// <param name="tn"></param>
        
/// <param name="parent"></param>
        protected void BindTreeItems(TreeNode tn, int parentid)
        {
            //取得当前登录用户id
            int userId = Session["uid"] != null ? int.Parse(Session["uid"].ToString()) : -1;
            List<Model.MenusInfo> listitem = new BLL.MenusInfo().GetModelList("MenuID=" + parentid);
            string sflag = "";
            if (listitem.Count > 0)
            {
                sflag = listitem[0].MenuName;
            }
            string sql  = " MenusInfo.MenuID in(select roleAuthority.MenuID from RoleAuthority roleAuthority where roleAuthority.RoleID in(select userRole.RoleID from UserRole userRole where userRole.UserID=" + userId + ")) and PreMenuID=" + parentid + "";
            List<Model.MenusInfo> list = new BLL.MenusInfo().GetModelList(sql);
            for (int i = 0; i < list.Count; i++)
            {
                TreeNode itemtn = new TreeNode();
                if (sflag.IndexOf ("台账管理")!=-1)
                {
                    itemtn.Text =list[i].MenuInfo+"-"+list[i].MenuName;
                }
                else
                {
                    itemtn.Text = list[i].MenuName;
                }
                itemtn.Value = list[i].MenuID.ToString();
                itemtn.Target = "mainFrame";
                itemtn.NavigateUrl = list[i].MenuAddress;
                tn.ChildNodes.Add(itemtn);
                //itemtn.ExpandAll();
                itemtn.Expanded = false;
            }
        }
        #region
        ///// <summary>
        
///// 绑定子节点
        
///// </summary>
        
///// <param name="tn"></param>
        
///// <param name="parent"></param>
        //protected void BindTreeItems(TreeNode tn, int parentid,int flag)
        
//{
        
//    //取得当前登录用户id
        
//    int userId = int.Parse(Session["uid"].ToString());
        
//    string sql = "";
        
//    if (flag == 0)
        
//    {
        
//        sql = " MenusInfo.MenuID in(select roleAuthority.MenuID from RoleAuthority roleAuthority where roleAuthority.RoleID in(select userRole.RoleID from UserRole userRole where userRole.UserID=" + userId + ")) and PreMenuID=" + parentid + "";
        
//    }
        
//    else if (flag == 1)
        
//    {
        
//        sql = " MenusInfo.MenuID in(select roleAuthority.MenuID from RoleAuthority roleAuthority where roleAuthority.RoleID in(select userRole.RoleID from UserRole userRole where userRole.UserID=" + userId + ")) and PreMenuID=" + parentid + " and MenuInfo='事项办理'";
        
//    }
        
//    else
        
//    {
        
//        sql = " MenusInfo.MenuID in(select roleAuthority.MenuID from RoleAuthority roleAuthority where roleAuthority.RoleID in(select userRole.RoleID from UserRole userRole where userRole.UserID=" + userId + ")) and PreMenuID=" + parentid + " and MenuInfo='台帐管理与统计分析'";
        
//    }
        
//    List<Model.MenusInfo> list = new BLL.MenusInfo().GetModelList(sql);
        
//    for (int i = 0; i < list.Count; i++)
        
//    {
        
//        TreeNode itemtn = new TreeNode();
        
//        itemtn.Text = list[i].MenuName;
        
//        itemtn.Value = list[i].MenuID.ToString();
        
//        itemtn.Target = "mainFrame";
        
//        itemtn.NavigateUrl =list[i].MenuAddress ;
        
//        tn.ChildNodes.Add(itemtn);
        
//        //itemtn.ExpandAll();
        
//        itemtn.Expanded = false;
        
//    }
        
//}
        #endregion
        protected void TVMenu_SelectedNodeChanged(object sender, EventArgs e)
        {
            for(int i=0;i<TVMenu .Nodes .Count;i++)
            {
                if (TVMenu.Nodes[i].Selected)
                {
                    TVMenu.Nodes[i].Expand();
                }
            }
        }

 

posted @ 2012-04-30 17:46  做最好の自己  阅读(1532)  评论(0编辑  收藏  举报