当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET 2005 Treeview终极解决方案

ASP.NET
ewebeditor在.net的使用方法
Server.Transfer,Response.Redirect的区别
ASP.NET2.0+SQL Server2005构建多层应用
ASP.NET 2.0 中收集的小功能点(转)
ASP.Net全局变量的设置和读取方法
数据库开发总结(ADO.NET小结)
ASP.net(c#)打造24小时天气预报及实时天气
发布WEB站点时出现Server Application Unavailable
在asp.net中实现datagrid checkbox 全选的方法
ASP.NET 2.0 URL映射技巧
ConfiguraionSource节点及多个配置文件的应用
SqlConnection.ConnectionString相关关键字
如何在WebForm中使用javascript防止连打(双击)
看到本质而不是现象--解决ASP.NET CS0016的问题
学会区分Visual Studio 2005,Visual Studio 2005 Team System和MSDN Premium 订阅的各个版本
ASP.NET 入门的五个步骤
ASP.NET 高性能分页代码
动态ItemTemplate的实现(译) - item,template
遍历Hashtable 的几种方法
通过VS中的数据源选择对话框简单实现数据库连接配置

ASP.NET 2005 Treeview终极解决方案


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 61 ::
收藏到网摘: n/a

  这几天在写HRM的时候 这问题搞了我两天,开始在使用Google 找了半天都是一堆垃圾,都是使用算法的较多, 后来就去了的msdn.yesky.com 找到点启示。 好了废话多说无用。
  首先表结构如下 表名 Test
按此在新窗口浏览图片

  写个存储过程 GetTreeview
  这个不用我说了吧下面用到
  为了速度缓存DataTable
Public Function GetTreeTable() As DataTable
 Dim dt As New DataTable()
 dt = HttpContext.Current.Cache("Treeview")
 If dt Is Nothing Then
  Dim Conn As New SqlConnection
  Dim clsConnDatabase As New ConnectionDatabase
  Conn = clsConnDatabase.ConnDatabase
  Dim Command As New SqlCommand
  Command.Connection = Conn
  Command.CommandText = "GetTreeview"
  Command.CommandType = CommandType.StoredProcedure
  Command.ExecuteNonQuery()
  Dim da As New SqlDataAdapter(Command)
  dt = New DataTable()
  da.Fill(dt)
  HttpContext.Current.Cache.Insert("Treeview", dt)
 End If
 Return dt
End Function
  这里是主要阿
Public Sub PopulateNodes(ByVal nodes As TreeNodeCollection, Optional ByVal intParentID As Int32 = 0)
 Dim dt As New DataTable()
 dt = clsWebForms.GetTreeTable()
 Dim strExpression As String
 strExpression = "[parentID] = " & intParentID
 Dim foundRows() As DataRow
 foundRows = dt.Select(strExpression)
 
 Dim I As Integer
 For I = 0 To foundRows.GetUpperBound(0)
  Dim tn As New TreeNode()
  tn.Text = foundRows(I).Item(“TableName”).ToString()
  tn.Value = foundRows(I).Item("ID").ToString()
  Dim dr() As DataRow
  dr = dt.Select("[parentID] = " & tn.Value)
  If dr.GetUpperBound(0) > -1 Then
   tn.PopulateOnDemand = True
  End If
  nodes.Add(tn)
 Next
End Sub
  建立WebForm 放入Treeview
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
 If Not Page.IsPostBack Then
  PopulateNodes(TreeView1.Nodes, 0)
 End If
End Sub
Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
 PopulateNodes(e.Node.ChildNodes, e.Node.Value)
End Sub

  至于速度我没测试,如果大家有兴趣帮忙测测。