Personally, I think it's good------use asp.net2.0 's TreeView control to display hierarchical data __.net

Source: Internet
Author: User
Tags new set
Now ASP. NET2.0 will release the official version, we--ASP. NET2.0 's developers--have long been interested in it. With the introduction of the new framework, ASP. A series of new controls are built into the NET2.0. One of my favorite controls in these controls is the TreeView control. (This article was published in 2005/10/31, Microsoft ASP.) NET2.0 officially launched in 2005/11/7, so there is a "forthcoming release of the official version," said Microsoft once in the ASP. Previous version of NET2.0 (ASP. NET1. X Edition) opened an open source code control-IE Web controls, based on the control and absorbing feedback from all sides, Microsoft launched the base. The NET2.0 TreeView control. The TreeView is not only rewritten, it also includes a new set of features, such as its support for client execution, dynamic filling of nodes, event postback, link navigation. ExampleI'm going to provide a simple example of how to use the TreeView control to display hierarchical data in a SQL Server database. Completing the Tiering tool does not require additional support for the database, which means that the TreeView should capture the hierarchical structure of the data to display-no matter how many layers the data is divided we assume that there is a name called the Treeviewsampledb database, which contains the samplecategories Database, which is structured as follows: Note: This example uses now, we use the Treeviewvb.aspx page, the following code shows the use of the TreeView <asp:treeview id= "TreeView1" expanddepth= "0 "Populatenodesfromclient=" true "showlines=" true "showexpandcollapse=" true "runat=" server "/> This code means that we set the TreeView The initialization stretch depth is a first-level node. We want to use the client node to perform special functions so that we can provide our customers with better service. Note that client postbacks are not used here.   In addition, if the node has child nodes, we also want to display the connector between the parent and child nodes, and use the stretch/Shrink icon to display the node's state. Next, we look at the background code file TreeViewVB.aspx.vb. We use code to complete the display of the root node, the following is part of the code:
protectedsub Page_Load (ByVal sender Asobject, ByVal e as System.EventArgs) handlesme.load
Ifnot Page.IsPostBack Then
Populaterootlevel ()
EndIf
Endsub
Privatesub Populaterootlevel ()
Dim objconn Asnew SqlConnection (_
"Server=joteke/sqlexpress; Trusted_connection=true;database=treeviewsampledb ")
Dim objcommand asnew SqlCommand ("Select Id,title, select COUNT (*) from Samplecategories" _
& "where Parentid=sc.id) Childnodecount from Samplecategories SC WHERE parentid is NULL", _
objconn)
Dim da Asnew SqlDataAdapter (objcommand)
Dim DT asnew DataTable ()
Da. Fill (DT)
Populatenodes (DT, treeview1.nodes)
Endsub
  above includes connecting the database, querying the first level node (that is, the parent node's ID is null), and using the Populatenodes method to establish the TreeNode object, the Populatenodes code is as follows: Privatesub Populatenodes (ByVal dt as DataTable, _  byval nodes as TreeNodeCollection)   foreach Dr as DataRow in DT. Rows     Dim tn asnew TreeNode ()     tn. Text = Dr ("title"). ToString ()     tn. Value = DR ("id"). ToString ()     nodes. Add (TN)       ' If node has child nodes, then enable On-demand populating     TN. PopulateOnDemand = (CInt (DR ("Childnodecount")) > 0)  next endsub   This piece of code is an important logical processing fragment, Populatenodes Method loops all rows in a DataTable, and if the given node has child nodes, it sets the PopulateOnDemand property to True. This way, if the node has child nodes, the stretch/Shrink icon is used to display the status. Next, we set up the child node of the code given node, that is, the user clicks the child node of the parent node, the code follows Privatesub populatesublevel (ByVal parentid Asinteger, _  byval parentnode As TreeNode)  dim objconn asnew SqlConnection (_    ) server=joteke/sqlexpress; Trusted_connection=true;database=treeviewsampledb ")  dim ObjcoMmand asnew SqlCommand ("Select Id,title," SELECT COUNT (*) from Samplecategories "_     &" WHERE Parenti d=sc.id) Childnodecount from Samplecategories SC where parentid= @parentID ", _     objconn)   Objcomma nd. Parameters.Add ("@parentID", SqlDbType.Int). Value = parentid    dim da asnew SqlDataAdapter (objcommand)  dim dt asnew DataTable ()   da. Fill (DT)   populatenodes (DT, parentnode.childnodes) endsub   The code snippet is executed in the same way as the root rating, but the slight difference is that only the given child nodes are queried and populated. The events that trigger the specified node are as follows: Protectedsub treeview1_treenodepopulate (ByVal sender Asobject, _  byval e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles treeview1.treenodepopulate   Populatesublevel CInt (E. Node.value), E.node) Endsub   that is, when the Ontreenodepopulate event is triggered, the Treeview1_treenodepopulate method is executed (ontreenodepopulate= "Treeview1_treenodepopulate"  ), the Treenodepopulate event is triggered when the user expands the parent node for the first time. Because of the populatenodesfromclient (TreeView) and PopulateOnDemand (TreeNode) settings, this allows the ASP.net page framework to capture the client's back, which makes the completion callback not necessarily a data postback. And because asp.net2.0 provides better cross-browser support, it works well in other browsers, such as Firefox. Note: The User click Node does not cause the postback of the data because I did not modify the data for the selected node. So, to run the results above, you can see that the results of the operation are as follows   This code involves two members of the TreeView, and you can view their descriptions on MSDN:   treeview.ontreenodepopulate MethodHttp://msdn2.microsoft.com/zh-CN/library/system.web.ui.webcontrols.treeview.ontreenodepopulate.aspx treeview.populatenodesfromclient PropertyHttp://msdn2.microsoft.com/zh-CN/library/system.web.ui.webcontrols.treeview.populatenodesfromclient.aspx SummaryHere we have quite simply completed the demo of the TreeView. This method requires some coding, of course, it is not necessary to use the TreeView to display the layered data aspect coding. In terms of displaying layered data, the TreeView provides us with a large active control for code writing. However, I believe that understanding the TreeView technology through code is the best way to learn it. code DownloadThe source code of this article comes with two versions of VB and C # language. The runtime can run Treeviewdbsqlschema.sql and treeviewdbsqldata.sql to build the database, or you can use the Treeviewsampledb.bak file to restore the database. Click the link below to download the source code for this article Source Text Addresshttp://aspalliance.com/732 SQL Server2000 Database, if you are using SQL Server2005 or SQL 2000 Previous versions of the database, this example also applies.
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.