asp.net the Final Solution of the TreeView

Source: Internet
Author: User
Tags final httpcontext table name tostring
These days in writing HRM This problem I two days, 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 msdn.yesky.com find some inspiration. All right, nonsense, more useless.

First table structure follows table name Test


Write a stored procedure Gettreeview

I don't need to tell you this.

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.