当前位置:网站首页>ASP.NET 读数据库绑定到 TreeView 递归方式
ASP.NET 读数据库绑定到 TreeView 递归方式
2022-07-28 05:23:00 【海乐学习】

创建表及插入模拟数据:
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)
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','','','','')在ASP.NET中读取数据绑定到TreeView实现代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<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.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
private readonly string ConnString = @"server=.\MSSQLSERVER2008;database=chart;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;
}
}边栏推荐
- Clustering of machine learning
- frameset 用法示例
- 基于选择性知识提取的野外低分辨率人脸识别的论文阅读笔记
- Overview of unconstrained low resolution face recognition III: homogeneous low resolution face recognition methods
- 生活随机-1
- UNL class diagram
- Cluster operation management system, to answer questions about the process
- 硬件电路设计学习笔记1--温升设计
- 《On Low-Resolution Face Recognition in the Wild:Comparisons and New Techniques》低分辨率人脸识别论文解读
- 51 single chip microcomputer independent key linkage nixie tube LED buzzer
猜你喜欢

Reading experience of protecting against DNN model steaming attacks

EMC实验实战案例-ESD静电实验

三、OpenVINO实战:图像分类

Small program development solves the anxiety of retail industry

TensorFlow2.1基本概念与常见函数

How much is wechat applet development cost and production cost?

基于直方图修改的可逆数字水印方法

ESXi社区版NVMe驱动更新v1.1

硬件电路设计学习笔记1--温升设计

一、语音合成与自回归模型
随机推荐
CVE_ 2017_ 11882 vulnerability recurrence (Metasploit opens NT remote desktop to add an account)
深度学习(自监督:MoCo v2)——Improved Baselines with Momentum Contrastive Learning
frameset 用法示例
What should we pay attention to when making template application of wechat applet?
ESXi社区版网卡驱动2022年3月更新
Deep learning (self supervision: Moco V2) -- improved bases with momentum contractual learning
深度学习数据窃取攻击在数据沙箱模式下的威胁分析与防御方法研究阅读心得
Apache log4j arbitrary code execution replication
Basic usage of word2vec and Bert
What is the detail of the applet development process?
端接电阻详解 信号完整系列 硬件学习笔记7
vscode uniapp
Nsctf web Title writeup
Overview of unconstrained low resolution face recognition III: homogeneous low resolution face recognition methods
EIGamal cryptosystem description
EMC实验实战案例-ESD静电实验
Deep learning pay attention to MLPs
Cluster operation management system, to answer questions about the process
5、 Video processing and GStreamer
《On Low-Resolution Face Recognition in the Wild:Comparisons and New Techniques》低分辨率人脸识别论文解读