悟生慧

 

TreeView控件如何设置节点显示与隐藏,主要是用来做后台权限,没有权限的就隐藏,有权限的就显示?

原文来自:http://wenwen.soso.com/z/q104168454.htm
using System;
using System.Data;
using System.Configuration;
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;

/// <summary>
/// TreeManage 的摘要说明
/// </summary>
public class TreeManage
{
public TreeManage()
{

}

//创建树
public void CreateTree(TreeView view, string RoleId)
{
CreateModule(view, RoleId);
}
//创建根结点
private void CreateModule(TreeView view, string RoleId)
{
Bll.ModuleBll mod = new Bll.ModuleBll();
DataTable dt = mod.SearchAllModule();
foreach (DataRow dr in dt.Rows)
{
TreeNode node = new TreeNode();
node.Text = dr["modName"].ToString();
node.Value = dr["modId"].ToString();
view.Nodes.Add(node);
node.Checked = CheckModuleRights(RoleId, dr["modId"].ToString());
CreatePage(node, RoleId); //生成某个模块节点的子节点
}
}
private void CreatePage(TreeNode node, string RoleId)
{
Bll.ItempBll bi = new Bll.ItempBll();
DataTable dt = bi.SearchAllPage(node.Value);
foreach (DataRow dr in dt.Rows)
{
TreeNode cnode = new TreeNode();
cnode.Text = dr["itemName"].ToString();
cnode.Value = dr["itemId"].ToString();
node.ChildNodes.Add(cnode);
cnode.Checked = CheckPageRights(RoleId, dr["itemId"].ToString());
}
}

private bool CheckModuleRights(string RoleId, string ModuleId)
{
Bll.ClerkModuleBll bcm = new Bll.ClerkModuleBll();
DataTable dt = bcm.SearchModuleRight(ModuleId, RoleId);
if (dt.Rows.Count > 0)
{
return true;
}
return false;
}
private bool CheckPageRights(string RoleId, string PageId)
{
Bll.ClerkItemBll bci = new Bll.ClerkItemBll();
DataTable dt = bci.SearchPageRight(PageId, RoleId);
if (dt.Rows.Count > 0)
{
return true;
}
return false;
}

public void CreateRights(TreeView view, string RoleId)
{
new Bll.ClerkItemBll().Delete(RoleId);
new Bll.ClerkModuleBll().Delete(RoleId);
CreateModuleRights(view, RoleId);
}
private void CreateModuleRights(TreeView view, string RoleId)
{
foreach (TreeNode node in view.Nodes)
{
Bll.ClerkModuleBll module = new Bll.ClerkModuleBll();
if (node.Checked == true)
{
//分配模块的权限
module.Insert(RoleId, node.Value);
CreatePageRights(node, RoleId);
}
}
}
private void CreatePageRights(TreeNode node, string RoleId)
{
Bll.ClerkItemBll item = new Bll.ClerkItemBll();
foreach (TreeNode cnode in node.ChildNodes)
{
if (cnode.Checked == true)
{
item.Insert(RoleId, cnode.Value);
}
}

}
}

posted on 2011-09-19 17:30  悟生慧  阅读(3161)  评论(0编辑  收藏  举报

导航