欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2022-01-03 12:03:11|栏目:.NET代码|点击:

复制代码 代码如下:

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


}

上一篇:ASP.NET中实现Form表单字段值自动填充到操作模型中

栏    目:.NET代码

下一篇:C#自定义HttpFilter模块完善实例

本文标题:关于ASP.NET中TreeView用法的一个小例子

本文地址:http://www.codeinn.net/misctech/188971.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有