当前位置:网站首页>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;
}
}边栏推荐
- 深度学习(增量学习)——(ICCV)Striking a Balance between Stability and Plasticity for Class-Incremental Learning
- USB Network Native Driver for ESXi更新到支持ESXi7.0 Update 2
- ESXi社区版NVMe驱动更新v1.1
- Reinforcement learning - proximal policy optimization algorithms
- Deep learning pay attention to MLPs
- Classification of iris based on Neural Network
- 压敏电阻设计参数及经典电路记录 硬件学习笔记5
- 监控安装ESXi on Arm的树莓派4b的CPU温度
- How much is wechat applet development cost and production cost?
- Overview of unconstrained low resolution face recognition I: data sets for low resolution face recognition
猜你喜欢

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

神经网络优化

What are the points for attention in the development and design of high-end atmospheric applets?

D2SC-GAN:基于双深浅通道生成对抗网络的课堂场景低分辨率人脸识别

Why is the kotlin language not popular now? What's your opinion?

CVE_ 2017_ 11882 vulnerability recurrence (Metasploit opens NT remote desktop to add an account)

Prime_ Series range from detection to weight lifting

Reinforcement learning - continuous control

On low resolution face recognition in the wild:comparisons and new technologies

电快速脉冲群(EFT)设计-EMC系列 硬件设计笔记4
随机推荐
基于选择性知识提取的野外低分辨率人脸识别的论文阅读笔记
ESXi on Arm 10/22 更新
Nanjing University of Posts and Telecommunications CTF topic writeup (II) including topic address
Scenario solution of distributed cluster architecture: cluster clock synchronization
Protecting Against DNN Model Stealing Attacks 论文阅读心得
Overview of unconstrained low resolution face recognition III: homogeneous low resolution face recognition methods
将GrilView中的数据转换成DataTable
How to improve the efficiency of small program development?
Prime_ Series range from detection to weight lifting
说说ESXi虚拟交换机和端口组的“混杂模式”
Four perspectives to teach you to choose applet development tools?
C语言EOF的理解
深度学习(一):走进机器学习与深度学习理论部分
Improved knowledge distillation for training fast lr_fr for fast low resolution face recognition model training
Deep learning (incremental learning) - iccv2022:continuous continuous learning
ESXi社区版网卡驱动再次更新
Latex入门
Geek challenge 2019-sql injection five questions PW
Adaface: quality adaptive margin for face recognition image quality adaptive edge loss for face recognition
5、 Video processing and GStreamer