ASP.NET 递归将分类绑定到 TreeView

原始问题来源:http://topic.csdn.net/u/20101213/10/e0616370-4790-4542-816e-2ee960defbc0.html

 

 

 

创建表及插入模拟数据:

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','','','','')

 

 

在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;
    }
}

 

posted on 2010-12-14 10:38  Ferry  阅读(5247)  评论(1编辑  收藏  举报

导航