Table Structure: Table Name Test
Write a stored procedure gettreeview
I don't need to talk about this. I will use it below.
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
Here is the main
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
Create a webform and put it in 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 treeviewinclutreenodepopulate (byval sender as object, byval e as system. Web. UI. webcontrols. treenodeeventargs) handles treeview1.treenodepopulate
Populatenodes (E. node. childnodes, E. node. value)
End sub