欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET TreeView读取数据库实例

程序员文章站 2024-02-28 22:18:34
前台:复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx...

前台:

复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="treeview._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="treeview1" runat="server" showlines="true">
        </asp:treeview>
    </div>
    </form>
</body>
</html>


后台:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.data;
using system.data.sqlclient;
using system.configuration;

namespace treeview
{
    public partial class _default : system.web.ui.page
    {
        public static string st = configurationmanager.connectionstrings["sql"].tostring();
        private datatable dts=new datatable();
        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
                dts = createtable();
                createnode();
            }
        }
        public void createnode()
        {
            datarow[] dr = dts.select("parentid=0");
            if(dr.length>0)
            {
                foreach(datarow drr in dr)
                {
                    treenode tn = new treenode();
                    tn.value = drr["menuid"].tostring();
                    tn.text = drr["menuname"].tostring();
                    tn.expanded = false;
                    tn.selectaction = treenodeselectaction.expand;
                    treeview1.nodes.add(tn);
                    createchild(tn,dts);
                }
            }
            else
            {
                treenode t=new treenode();
                t.value="空";
                t.text="空";
                t.expanded=false;
                t.selectaction=treenodeselectaction.expand;
                treeview1.nodes.add(t);
            }
        }
        public void createchild(treenode tnn, datatable dtt)
        {
            datarow[] dr = dtt.select("parentid=" + tnn.value);
            if (dr.length > 0)
            {
                foreach (datarow drw in dr)
                {
                    treenode ts = new treenode();
                    ts.value = drw["menuid"].tostring();
                    ts.text = drw["menuname"].tostring();
                    ts.selectaction = treenodeselectaction.expand;
                    ts.expanded = false;
                    tnn.childnodes.add(ts);
                    createchild(ts, dtt);
                }
            }
        }
        public datatable createtable()
        {
            datatable d = new datatable();
            using(sqlconnection sql=new sqlconnection(st))
            {

                sqlcommand sq=new sqlcommand("select * from treeviewname",sql);
                sqldataadapter sda=new sqldataadapter();
                sda.selectcommand = sq;
                sda.fill(d);

            }
            return d;
        }
    }
}