asp.net|treeview| recursion | solve
Cache DataTable for Speed
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
This is the main one.
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
Build WebForm into the 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
As for speed I did not test, if you are interested to help test.
Off the topic (suddenly found himself old, 26 years old wrote Fast 7 years of the program has been using various versions of VB, and Masm (MS Macro assembly language) in the VB 6.0 times around 2000 years to set up a VB Programming main personal homepage programming prodigal son) by Shadow
Note: This paper has been transferred from Http://blog.csdn.net/shadowsky/archive/2006/06/18/811461.aspx
These days in writing HRM time This problem has done me two days, haha! Started in the use of Google to find a long time is a pile of garbage, are using more algorithms, and then went to the www.asp.net to find some inspiration. All right, nonsense, more useless. First the table structure follows the table name Test write a stored procedure gettreeview this don't need me to say it.