ASP.NET 讀資料庫綁定到 TreeView 遞迴方式

來源:互聯網
上載者:User

建立表及插入類比資料:

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;    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.