sponsored links

Treeview 无限分类非递归终极解决方案C#

后台代码部分:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;
using System.Data.SqlClient;
using LTP.Common;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateNodes(TreeView1.Nodes, 0);
        }//     
    }

    public DataTable GetTreeTable()
    {
        DataTable dt = new DataTable();
        dt = HttpContext.Current.Cache["Treeview"] as DataTable;
        if (dt != null)
        {
            //此处是数据库连接
            //String ConnStr = LTP.Common.ConfigHelper.GetConfigString("DNS");

            String ConnStr = ConfigurationManager.ConnectionStrings["DNS"].ConnectionString;

            SqlConnection Conn = new SqlConnection(ConnStr);

            //String sqlstr = "select * from TreeJi";

            SqlCommand Com = new SqlCommand();

            Com.CommandText = "select * from TreeJi";
            Com.Connection = Conn;

            Conn.Open();
            Com.ExecuteNonQuery();

            SqlDataAdapter sda = new SqlDataAdapter(Com);
            dt = new DataTable();
            sda.Fill(dt);
            HttpContext.Current.Cache.Insert("Treeview", dt);          
            Conn.Close();
            Conn.Dispose();
        }

        return dt;

    }

    public void PopulateNodes(TreeNodeCollection nodes, Int32 intParentID)
    {
        DataTable dt = new DataTable();
        dt = this.GetTreeTable();

        String strExpression = String.Empty;

        strExpression = "[parentID]=" + intParentID;
        System.Data.DataRow[] dr;

        dr = dt.Select(strExpression);

        for (Int32 i = 0; i <= dr.GetUpperBound(0); i++)
        {

            TreeNode tn = new TreeNode();           //Dim tn As New TreeNode()

            tn.Text = dr[i]["TableName"].ToString().Trim();//tn.Text = foundRows(I).Item(“TableName”).ToString()

            tn.Value = dr[i]["ID"].ToString();  //tn.Value = foundRows(I).Item("ID").ToString()

            DataRow[] dr1;  //Dim dr() As DataRow

            dr1 = dt.Select("[parentID]=" + tn.Value);  //dr = dt.Select("[parentID] = " & tn.Value)

            if (dr1.GetUpperBound(0) > -1)
            {
                tn.PopulateOnDemand = true;
            }//If dr.GetUpperBound(0) > -1 Then

            //    tn.PopulateOnDemand = True

            //End If

            nodes.Add(tn);            

        }

        //TreeView1.DataSource = dt.DefaultView;
        //TreeView1.DataBind();
    }

    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
       
        PopulateNodes(e.Node.ChildNodes, Int32.Parse(e.Node.Value));
    }
}

前台页面部分:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TreeView ID="TreeView1" runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate">
      </asp:TreeView>
    </div>
    </form>
</body>
</html>
 
Tags:
  • ASP.net 2005 Treeview 无限分类非递归终极解决方案
    ASP.net 2005 Treeview 无限分类非递归终极解决方案
        ASP.net 2005 Treeview 无限分类非递归终极解决方案 这几天在写HRM的时候 这问题搞了我两天,!开始在使用Google 找了半天都是一堆垃圾,都是使用算法的较多, 后来就去了www.asp.net 找到点启示. 好了废话多说无用. 首先表结构如下 表名 Test   http://blog.csdn.net/shadowsky/a ...
  • ASP.net 2005 Treeview 无限分类非地递归终极解决方案
    ASP.net 2005 Treeview 无限分类非地递归终极解决方案
    这几天在写HRM的时候 这问题搞了我两天,!开始在使用Google 找了半天都是一堆垃圾,都是使用算法的较多, 后来就去了www.asp.net 找到点启示. 好了废话多说无用. 首先表结构如下 表名 Test   写个存储过程 GetTreeview 这个不用我说了吧下面用到   为了速度缓存DataTable         Public Functio ...
  • 转:php+mysql菜单无限级分类(非递归)
    php+mysql无限级分类(非递归) 参考:http://www.chhua.com/web-note3244
  • thinkphp实现无限分类使用递归
    thinkphp实现无限分类使用递归
    这篇文章主要介绍了在使用递归的情况下thinkphp实现无限分类,感兴趣的小伙伴们可以参考一下本文实例为大家分享了thinkphp实现无限分类的详细代码,希望对大家学习无限分类有所启发. 数据库:test数据表:(tp_category): Common/conf/config.php 'DB_CONFIG2' => array( 'db_type' ...
  • TreeView绑定数据库 非递归
      数据库: 公司:id(int)     company(varchar(50) 部门:id(int)     deid(int)     department(varchar(50)) 班组:id(int)     coid(int)     class(varchar(50)) 非递归实现         string strConn = System ...