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

TreeView控件的绑定

程序员文章站 2022-07-02 18:34:59
using system; using system.collections.generic; using system.linq; using system.text;...
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.data;
using system.web;
using system.web.ui.webcontrols;
using dal;
using model;
 
namespace bll
{
    public class varitymanage
    {
        //获得根结点信息
        public static list<pnode> getpnode()
        {
            return dal.listtreenode.getpnode(); 
 
        }
        //获得叶结点信息
        public static list<node> getnode(int pnode_id)
        {
            return dal.listtreenode.getnode(pnode_id);
        }
 
    }
}
 
关于treeview控件的绑定,在网上查了很多资料,练习了在两种情况下的绑定,一种是在网页后台直接绑定,一种是在三层架构中绑定。
 
表的设计

 商品小类表(tb_type)
字段名 数据类型 主键 外键 非空 含义
type_id int 小类编号
type_name nvarchar(50) 小类名称
category_id int 大类编号
intro text 介绍
 商品大类表(tb_ category)
字段名 数据类型 主键 外键 非空 含义
category_id int 大类编号
category_name varchar(255) 大类名称
intro text 介绍
 
网页后台直接绑定:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using mysql.data.mysqlclient;
using system.data;
public partial class _default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        if (!ispostback)
        {
            datatable dtfather=exe_table("server=localhost;user id=root;password=root;database=em", "select distinct tb_category.category_id,tb_category.category_name from   tb_category");
            foreach(datarow dr in dtfather.rows)
            {
                 
                treenode tn = new treenode();
                tn.value = dr["category_id"].tostring();
                tn.text = dr["category_name"].tostring();
                treeview1.nodes.add(tn);
                datatable dtchild = exe_table("server=localhost;user id=root;password=root;database=em",
                    "select type_id,type_name,category_id from tb_type where tb_type.category_id=" + dr["category_id"].tostring());
                foreach (datarow drchild in dtchild.rows)
                {
                    treenode tnchild = new treenode();
                    tnchild.value = drchild["type_id"].tostring();//节点的value值,一般为数据库的id值
                    tnchild.text = drchild["type_name"].tostring();//节点的text,节点的文本显示
                    tn.childnodes.add(tnchild);
                }
 
            }
        }
    }
 
 
 
          /// <summary>
        /// 取出数据库中数据,生成datatable
        /// </summary>
        /// <param name="str_con">数据库连接</param>
        /// <param name="str_cmd">sql语句</param>
        /// <returns></returns>
        private datatable exe_table(string str_con,string str_cmd)
        {
            dataset ds = new dataset();
            using (mysqlconnection conn = new mysqlconnection(str_con))
            {
                using (mysqldataadapter oda = new mysqldataadapter(str_cmd, conn))
                {
                    conn.open();
                    oda.fill(ds);
                }
            }
            return ds.tables[0];
        }
 
 
}
 
 
三层架构绑定:
model实体类:

using system;
using system.collections.generic;
using system.linq;
using system.text;
 
namespace model
{
    public class pnode
    {
        public int id { get; set; }
        public string name { get; set; }
        public string intro { get; set; }
    }
}
 
 

using system;
using system.collections.generic;
using system.linq;
using system.text;
 
namespace model
{
    public class node
    {
        public int id { get; set; }
        public string name { get; set; }
        public int pid { get; set; }
        public string intro { get; set; }
    }
}
 
dal:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using mysql.data.mysqlclient;
using system.data;
using model;
namespace dal
{
    public class listtreenode
    {
        public static list<pnode> getpnode()
        {
            string strsql = "select distinct category_id,category_name ,intro from tb_category";
            list<pnode> list = new list<pnode>();
            using (mysqlconnection cn = new mysqlconnection(strcon.conn))
            {
                cn.open();
                mysqldataadapter sda = new mysqldataadapter(strsql, cn);
                dataset ds = new dataset();
                sda.fill(ds);
                foreach (datarow dr in ds.tables[0].rows)
                {
                    pnode pnode = new pnode();
                    pnode.id = (int)dr["category_id"];
                    pnode.name = dr["category_name"].tostring();
                    pnode.intro = dr["intro"].tostring();
                    list.add(pnode);
                }
                return list;
            }
        }
 
        public static list<node> getnode(int pnode_id)
        {
            string strsql ="select type_id,type_name,tb_type.intro,category_id from tb_type where tb_type.category_id=" + pnode_id;
            list<node> list = new list<node>();
            using (mysqlconnection cn = new mysqlconnection(strcon.conn))
            {
                cn.open();
                mysqldataadapter sda = new mysqldataadapter(strsql, cn);
                dataset ds = new dataset();
                sda.fill(ds);
                foreach (datarow dr in ds.tables[0].rows)
                {
                    node node = new node();
                    node.id = (int)dr["type_id"];
                    node.name = dr["type_name"].tostring();
                    node.intro = dr["intro"].tostring();
                    list.add(node);
                }
                return list;
            }
        }
    }
}
 
bll:
 

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.data;
using system.web;
using system.web.ui.webcontrols;
using dal;
using model;
 
namespace bll
{
    public class varitymanage
    {
        //获得根结点信息
        public static list<pnode> getpnode()
        {
            return dal.listtreenode.getpnode(); 
 
        }
        //获得叶结点信息
        public static list<node> getnode(int pnode_id)
        {
            return dal.listtreenode.getnode(pnode_id);
        }
 
    }
}
 
ui:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using bll;
using model;
 
namespace e_mall_background_system.management
{
    public partial class main : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {
            if (!ispostback)
            {
                bindtreeview();
                bindddlselectpnode1();
            }
        }
 
        //treeview的绑定
        public void bindtreeview()
        {
            treeview1.nodes.clear();
            int rootid = 0;
            string rootname = "所有商品种类";
            treenode root = createtreenode(rootid, rootname);
            treeview1.nodes.add(root);
            list<pnode> pnodes = varitymanage.getpnode();
            foreach (pnode pnode in pnodes)
            {
                int nodeid = (int)pnode.id;
                string nodename = pnode.name.tostring();
                treenode treenode = this.createtreenode(nodeid, nodename);
                root.childnodes.add(treenode);
                list<node> nodes = varitymanage.getnode(nodeid);
                foreach (node node in nodes)
                {
                    int childnodeid =(int) node.id;
                    string childnodename = node.name.tostring();
                    treenode childtreenode = this.createtreenode(childnodeid, childnodename);
                    treenode.childnodes.add(childtreenode);
                }
                 
            }
        }
 
        public treenode createtreenode(int id, string name)
        {
            treenode tn = new treenode();
            tn.value = id.tostring();
            tn.text = name;
            return tn;
        }
 
    }
}


摘自 miko2012