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

ASP.NET TreeView动态绑定数据库显示

程序员文章站 2022-05-20 18:13:47
下面是一个treeview动态的绑定3层深度的树的代码,具体的显示请查看下面网站: 点击打开链接 using system; using system.collections; using sy...

下面是一个treeview动态的绑定3层深度的树的代码,具体的显示请查看下面网站:

点击打开链接

using system;
using system.collections;
using system.configuration;
using system.data;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.data.sqlclient;
using common;
using dataaccess;

public partial class controls_producttype2 : system.web.ui.usercontrol
{


    producttype producttype = new producttype();
    product product = new product();



  protected void treeview_treenodepopulate(object sender, treenodeeventargs e)
    {
      
        if (e.node.childnodes.count == 0)
        {
            switch (e.node.depth)
            {
                case 0:
                    populatecategories(e.node);
                    break;
                case 1:
                    populateproducttype(e.node);
                    break;
                case 2:
                    populateproducts(e.node);
                    break;
            }
        }

      //  int i = treetoxml.treetoxml(treeview1, "d:/treetoxml.xml");
        
    }


    protected void page_load(object sender, eventargs e)
    {

    }

    //第一级节点(parentid=0)
    void populatecategories(treenode node)
    {
        datatable dt = new datatable();
        dt = producttype.getlistbyfathernode();

        if (dt.rows.count > 0)
        {
            foreach (datarow row in dt.rows)
            {
                treenode newnode = new treenode(row["producttypename"].tostring(), row["producttypeid"].tostring(),"","../webproductlist.aspx?producttypeidone="+ row["producttypeid"].tostring() +"","");
                newnode.populateondemand = true;
                newnode.selectaction = treenodeselectaction.expand;
                node.childnodes.add(newnode);
                node.expanded = true ;
            }
        }

    }


    //第二级节点
    void populateproducttype(treenode node)
    {
        datatable dt = new datatable();
        queryparam queryparam = new queryparam();
        queryparam.add("parentid", queryparam.relation.eq, "" + node.value + "");
        dt = producttype.getlistbybranchnode(queryparam);

        if (dt.rows.count > 0)
        {
            foreach (datarow row in dt.rows)
            {
                treenode newnode = new treenode(row["producttypename"].tostring(), row["producttypeid"].tostring(), "", "../webproductlist.aspx?producttypeidtwo=" + row["producttypeid"].tostring() + "", "");
                newnode.populateondemand = true;
                newnode.selectaction = treenodeselectaction.expand;
                node.childnodes.add(newnode);
                node.expanded = false ;
            }
        }
    }



    //第三个节点
    void populateproducts(treenode node)
    {
        datatable dt = new datatable();
        queryparam queryparam = new queryparam();
        queryparam.add("producttypeitemid", queryparam.relation.eq, "" + node.value + "");
        dt = product.getlist(queryparam);

        if (dt.rows.count > 0)
        {
            foreach (datarow row in dt.rows)
            {
                treenode newnode = new treenode(row["productname"].tostring(), row["productid"].tostring(), "", "../webproductdetaile.aspx?productid=" + row["productid"].tostring() + "", "");
                //newnode.populateondemand = false;
                //newnode.selectaction = treenodeselectaction.none;
                newnode.populateondemand =true ;
                newnode.selectaction = treenodeselectaction.expand ;
                node.childnodes.add(newnode);
                node.expanded = false;
            }
        }
    }


}