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

发布时间:2020-06-24 17:44:18 作者:xiaoxuanyunmeng
来源:网络 阅读:576

下面是一个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);
       unity3d http://www.unitymanual.com/

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


}

推荐阅读:
  1. php中一个“冒泡排序”的小例子
  2. 一个php多态性的小例子

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

朋友 动态 product

上一篇:layui中的table字段筛选功能怎么用

下一篇:python字典相加的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》