当前位置: 首页 > news >正文

动态权限树控件

后台代码:
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 System.Data.SqlClient;
public partial class control_TreeViewControl : System.Web.UI.UserControl
{
    private int opclass = 1;//默人权限最小为个人
    /// <summary>
    /// 修改opclass的属性
    /// </summary>
    public int Opclass
    {
        get { return opclass; }
        set { opclass = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Opclass = Session["OptClass"].ToString();//从session中取得权限
        if (!IsPostBack)
        {
            TreeNode trObj = new TreeNode("首页", "00");
            trObj.NavigateUrl = "../Default.aspx";
            //trObj.Target = "main";
            trObj.PopulateOnDemand = true;//动态填充设置为true
            TreeViewlist.Nodes.Add(trObj);
        }
    }

    private void PopulateFirst(TreeNode node)
    {
        // Query for the product categories. These are the values
        // for the second-level nodes.
        DataSet ResultSet = RunQuery("Select ModuleName,ModuleID From fn_visit_table(" + opclass + ") Where len(ModuleID)=3 and ModuleId like '21_'");//加载一级菜单
        // Create the second-level nodes.
        if (ResultSet.Tables.Count > 0)
        {
            // Iterate through and create a new node for each row in the query results.
            // Notice that the query results are stored in the table of the DataSet.
            foreach (DataRow row in ResultSet.Tables[0].Rows)
            {
                // Create the new node. Notice that the CategoryId is stored in the Value property
                // of the node. This will make querying for items in a specific category easier when
                // the third-level nodes are created.
                TreeNode newNode = new TreeNode();
                newNode.Text = row["ModuleName"].ToString();
                newNode.Value = row["ModuleID"].ToString();
                // Set the PopulateOnDemand property to true so that the child nodes can be
                // dynamically populated.
                newNode.PopulateOnDemand = true;
                // Set additional properties for the node.
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                // Add the new node to the ChildNodes collection of the parent node.
                node.ChildNodes.Add(newNode);
            }
        }
    }
    private void PopulateSecond(TreeNode node)
    {
        // Query for the products of the current category. These are the values
        // for the third-level nodes.
        DataSet ResultSet = RunQuery("Select ModuleName,ModuleID,Remark From fn_visit_table(" + opclass + ")" +
            " Where len(ModuleID)=5 and substring(convert(char(8),ModuleID),1,3)='" + node.Value + "'");//加载二级菜单
        // Create the third-level nodes.
        if (ResultSet.Tables.Count > 0)
        {
            // Iterate through and create a new node for each row in the query results.
            // Notice that the query results are stored in the table of the DataSet.
            foreach (DataRow row in ResultSet.Tables[0].Rows)
            {
                // Create the new node.
                TreeNode newNode = new TreeNode();
                newNode.Text = row["ModuleName"].ToString();
                newNode.Value = row["ModuleID"].ToString();
                newNode.NavigateUrl = row["Remark"].ToString();
                // Set the PopulateOnDemand property to false, because these are leaf nodes and
                // do not need to be populated.
                newNode.PopulateOnDemand = true;
                // Set additional properties for the node.
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                // Add the new node to the ChildNodes collection of the parent node.
                node.ChildNodes.Add(newNode);
            }
        }
    }

    private void PopulateThird(TreeNode node)
    {
        // Query for the products of the current category. These are the values
        // for the third-level nodes.
        DataSet ResultSet = RunQuery("Select ModuleName,ModuleID,Remark From fn_visit_table(" + opclass + ")" +
            " Where len(ModuleID)=8 and substring(convert(char(8),ModuleID),1,5)='" + node.Value + "'");加载三级菜单
        // Create the third-level nodes.
        if (ResultSet.Tables.Count > 0)
        {
            // Iterate through and create a new node for each row in the query results.
            // Notice that the query results are stored in the table of the DataSet.
            foreach (DataRow row in ResultSet.Tables[0].Rows)
            {
                // Create the new node.
                TreeNode newNode = new TreeNode();
                newNode.Text = row["ModuleName"].ToString();
                newNode.Value = row["ModuleID"].ToString();
                newNode.NavigateUrl = row["Remark"].ToString();
                // Set the PopulateOnDemand property to false, because these are leaf nodes and
                // do not need to be populated.
                newNode.PopulateOnDemand = false;
                // Set additional properties for the node.
                newNode.SelectAction = TreeNodeSelectAction.Expand;//
                // Add the new node to the ChildNodes collection of the parent node.
                node.ChildNodes.Add(newNode);
            }
        }
    }
    private DataSet RunQuery(String QueryString)
    {
        String ConnectionString = ConfigurationManager.ConnectionStrings["MISDBConnectionString"].ToString();
        SqlConnection DBConnection = new SqlConnection(ConnectionString);
        SqlDataAdapter DBAdapter;
        DataSet ResultsDataSet = new DataSet();
        try
        {
            //运行查询和创建DataSet
            DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
            DBAdapter.Fill(ResultsDataSet);
            DBConnection.Close();//关闭连接
        }
        catch
        {
            // 异常处理如果连接没有关闭,则关闭连接.
            if (DBConnection.State == ConnectionState.Open)
            {
                DBConnection.Close();
            }
            throw new Exception();
        }
        return ResultsDataSet;
    }
    protected void tvClass_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        // Call the appropriate method to populate a node at a particular level.
        int myTemp = e.Node.Depth;
        switch (myTemp)
        {
            case 0:
                // 装载第一层节点
                PopulateFirst(e.Node);
                break;
            case 1:
                // 装载第二层节点
                PopulateSecond(e.Node);//PopulateSecond
                break;
            case 2:
                // 装载第三层节点
                PopulateThird(e.Node);
                break;
            default:
                // Do nothing.
                break;
        }
    }
}
前台代码:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TreeViewControl.ascx.cs" Inherits="control_TreeViewControl" %>
<asp:TreeView ID="TreeViewlist" runat="server" OnTreeNodePopulate="tvClass_TreeNodePopulate" CssClass="menutextindent" SkinID="TreeView">
</asp:TreeView>
效果图:

转载于:https://www.cnblogs.com/hemingway/archive/2007/07/17/820822.html

相关文章:

  • 南桥-- 算法训练 2的次幂表示
  • 第九章:XML文档集成---AxInternalBase API
  • 微信登陆
  • 文本相似度计算--余弦定理和广义Jaccard系数
  • 结构型模型Bridge
  • PHP实现程序单例执行
  • Visual C#.Net网络程序开发-Tcp篇(3)
  • [New Portal]Windows Azure Virtual Machine (3) 在VM上挂载磁盘
  • ajax.net 我曾经轻视了他,郁闷。
  • 彻底去除Win10“快速访问”
  • 网络服务器时间发布地址
  • 《Python地理数据处理》——2.3 变量
  • 只能使用数组初始值设定项表达式为数组类型赋值。请尝试改用新的表达式
  • 《Total Commander:万能文件管理器》——12.3.高人高见
  • 在C#中应用哈希表(Hashtable)
  • SegmentFault for Android 3.0 发布
  • [case10]使用RSQL实现端到端的动态查询
  • [译] React v16.8: 含有Hooks的版本
  • 【Leetcode】104. 二叉树的最大深度
  • django开发-定时任务的使用
  • ES6 学习笔记(一)let,const和解构赋值
  • Golang-长连接-状态推送
  • Less 日常用法
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • Nodejs和JavaWeb协助开发
  • PV统计优化设计
  • spring-boot List转Page
  • Theano - 导数
  • vue中实现单选
  • 数组大概知多少
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 小程序01:wepy框架整合iview webapp UI
  • 源码安装memcached和php memcache扩展
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • # Java NIO(一)FileChannel
  • (003)SlickEdit Unity的补全
  • (4)Elastix图像配准:3D图像
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (翻译)terry crowley: 写给程序员
  • (附源码)springboot 基于HTML5的个人网页的网站设计与实现 毕业设计 031623
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (循环依赖问题)学习spring的第九天
  • (一)appium-desktop定位元素原理
  • (转)淘淘商城系列——使用Spring来管理Redis单机版和集群版
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .md即markdown文件的基本常用编写语法
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET Core跨平台微服务学习资源
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .NET delegate 委托 、 Event 事件,接口回调