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

关于ASP.NET中TreeView用法的一个小例子

程序员文章站 2024-02-26 15:26:34
复制代码 代码如下:using system;using system.collections;using system.configuration;using syste...

复制代码 代码如下:

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;
            }
        }
    }


}