惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
月光博客
月光博客
博客园 - 司徒正美
爱范儿
爱范儿
aimingoo的专栏
aimingoo的专栏
量子位
Recent Announcements
Recent Announcements
V
V2EX
P
Proofpoint News Feed
小众软件
小众软件
云风的 BLOG
云风的 BLOG
腾讯CDC
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
B
Blog
博客园_首页
GbyAI
GbyAI
博客园 - Franky

博客园 - Ferry

ASP.NET网站权限设计实现(三)——套用JQuery EasyUI列表显示数据、分页、查询 ASP.NET网站权限设计实现(二)——角色权限绑定 ASP.NET读取网络图片并在页面上显示 ASP.NET网站权限设计实现(一)——使用PowerDesigner进行数据库设计 Visual Studio 2010 架构图之用例图(UML Use Case Diagram) ASP.NET MVC3 新的视图引擎(View Engine) 一个德国人图解中西文化差异(值得一看) 微软AJAX CDN(内容分发网络) 注册使用GAC—Global Assembly Cache(.NET) 新浪微博PC客户端(DotNet WinForm版)——功能实现分解介绍 新浪微博PC客户端(DotNet WinForm版)—— 初探 C#:30行数据插入到数据库中的效率测试-一行行执行、构造SQL一次执行、SqlBulkCopy C#使用GMAIL群发带附件邮件的例子 ASP.NET Web 应用开发实战快速上手系列 3—C#面向对象编程纲要-类 ASP.NET带进度条多文件上传 ASP.NET4.0中客户端ID的生成 在高优先级下运行应用程序 学习使用NHibernate2.1.0Beta1(续七)— 单例模式 ASP.NET Web 应用开发实战快速上手系列 2——C#基础
ASP.NET 递归将分类绑定到 TreeView - Ferry
Ferry · 2010-12-14 · via 博客园 - Ferry

CREATE TABLE [dbo].[sysMenuTree](
[NoteId] [decimal](180NOT NULL,
[ParentId] [decimal](180NULL,
[sText] [nvarchar](50NULL,
[sValue] [nvarchar](50NULL,
[sURL] [nvarchar](50NULL,
[sTarget] [nvarchar](50NULL,
[Chger] [nvarchar](50NULL,
[ChgTime] [nvarchar](50NULL,
 
CONSTRAINT [PK_sysMenuTree] PRIMARY KEY CLUSTERED  
(
[NoteId] ASC
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]
ON [PRIMARY]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','','','','')

<%@ 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.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;
    }
}