• ASP.NET 读数据库绑定到 TreeView 递归方式


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TreeView ID="treeMenu" runat="server"></asp:TreeView>
        </div>
        </form>
    </body>
    </html>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    
    public partial class _Default : System.Web.UI.Page
    {
        private readonly string ConnString = @"server=localhost;database=Tree;uid=sa;pwd=123456";
        private DataTable dt = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dt = new DataTable();
                GetMenuToDataTable("select * from sysMenuTree", dt);
                BindTree(dt, null, "0");
            }
        }
    
        private void BindTree(DataTable dtSource, TreeNode parentNode, string parentID)
        {
            DataRow[] rows = dtSource.Select(string.Format("ParentID={0}", parentID));
            foreach (DataRow row in rows)
            {
                TreeNode node = new TreeNode();
                node.Text = row["sText"].ToString();
                node.Value = row["sValue"].ToString();
                BindTree(dtSource, node, row["NoteId"].ToString());
                if (parentNode == null)
                {
                    treeMenu.Nodes.Add(node);
                }
                else
                {
                    parentNode.ChildNodes.Add(node);
                }
            }
        }
    
        private DataTable GetMenuToDataTable(string query, DataTable dt)
        {
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                SqlCommand cmd = new SqlCommand(query, conn);
                SqlDataAdapter ada = new SqlDataAdapter(cmd);
                ada.Fill(dt);
            }
            return dt;
        }
    }
    CREATE TABLE [sysMenuTree](
    [NoteId] [decimal](18, 0) NOT NULL,
    [ParentId] [decimal](18, 0) NULL,
    [sText] [nvarchar](50) NULL,
    [sValue] [nvarchar](50) NULL,
    [sURL] [nvarchar](50) NULL,
    [sTarget] [nvarchar](50) NULL,
    [Chger] [nvarchar](50) NULL,
    [ChgTime] [nvarchar](50) NULL)
    
    create database Tree
    
    insert into sysMenuTree values(3,0,N'目录',N'目录','','','','')
    insert into sysMenuTree values(4,0,N'目录',N'目录','','','','')
    insert into sysMenuTree values(5,0,N'目录',N'目录','','','','')
    insert into sysMenuTree values(6,3,N'项目.1',N'项目.1','','','','')
    insert into sysMenuTree values(7,3,N'项目.2',N'项目.2','','','','')
    insert into sysMenuTree values(8,4,N'项目.1',N'项目.1','','','','')
    insert into sysMenuTree values(9,4,N'项目.2',N'项目.2','','','','')
    insert into sysMenuTree values(10,4,N'项目.3',N'项目.3','','','','')
    insert into sysMenuTree values(11,5,N'项目.1',N'项目.1','','','','')
    insert into sysMenuTree values(12,5,N'项目.2',N'项目.2','','','','')
  • 相关阅读:
    Spyder的汉化
    Python,Pycharm,Anaconda等的关系与安装过程~为初学者跳过各种坑
    好了,我的第一篇博客!
    Xcode 最低要求和支持的 SDK
    python连接hive (安装impyla)的采坑之旅
    java泛型(泛型接口、泛型类、泛型方法)
    oracle命令查看表结构及表索引
    Linux环境下安装、配置Nginx1.14.2(CentOS Linux release 7.6.1810)
    Caffe入门随笔
    Gradient Boosting算法简介
  • 原文地址:https://www.cnblogs.com/XACOOL/p/5646024.html
Copyright © 2020-2023  润新知