asp.net the ultimate solution to infinite classification of the Treeview

Source: Internet
Author: User
Tags httpcontext table name tostring
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.

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.