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

创建表及插入模拟数据:

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">
  
<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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值