Hierarchical loading of Treeview

Source: Internet
Author: User
Let's talk about the main principles:
Here, we use the ontreenodepopulate event and refresh the data. When loading for the first time, obtain the root node and its two levels of nodes, display the root node and the first level node, use the second level node to determine whether the first level node contains the knot node, and cache the second level node. Fill in the cached second-level node for the next step-by-step extension, and obtain all the child nodes of the second-level node.

Note: Why is second-level data required?
Because after obtaining the first level of data, you do not know whether there is a lower level. Therefore, obtain the next-level data after the first-level data and use the nodedictionary variable to cache all second-level data for next loading.
Next time you click "extension", first find the data in this level from the variable, and then read the data in the next level.

The principle is boring. Examples are as follows:

1. Add Treeview to the page. Set the ontreenodepopulate event and enableclientscript attributes.

Code < ASP: Treeview ID = "Tvdepartment" Runat = "Server" Nodeindent = "10"
Ontreenodepopulate = "Tvdepartment_treenodepopulate"
Enableclientscript = "False" >
< Parentnodestyle Forecolor = "# 5555dd" />
< Selectednodestyle Backcolor = "# F2f6fb"   />
< Nodestyle Font-size = "12px" Forecolor = "Black"   />
</ ASP: Treeview >

The enableclientscript attribute is described here. If enableclientscript = "true", the client's corner book will be called when you click the extension endpoint and will not be sent back. The retrieved value is inconsistent with the value of the current vertex, resulting in an error. Therefore, set the enableclientscript attribute to true.

 

2. BackgroundCode(The method for getting data in the Code is not provided. You can write the data retrieval method as needed)

Code // Used to cache data
Private   Static Dictionary < String , Ilist < Pw_department > Nodedictionary =   New Dictionary < String , Ilist < Pw_department > ();
Protected   Void Page_load ( Object Sender, eventargs E)
{
If ( ! Page. ispostback)
{
Nodedictionary. Clear ();
Binddepartmenttree ( This . Pwpid, " System " );
}
}
// Obtain the first level of data in the root directory
Public   Void Binddepartmenttree ( String Pwpid, String Departmentno)
{
Nodedictionary. Clear ();
Userpower =   New Userpower ();
// Get the root directory
Pw_department pwsystem = Userpower. getparentdepartment (pwpid, departmentno );
Tvdepartment. nodes. Clear ();
If (Pwsystem =   Null )
{
Return ;
}
Treenode tnrootnode =   New Treenode ();
Tnrootnode. Text = Pwsystem. departmentname;
Tnrootnode. Value = Pwsystem. departmentno;
// Obtain the first level of data in the root directory
Ilist < Pw_department > Ilpwdepts = Userpower. getchilddepartment (pwpid, departmentno );
If (Ilpwdepts =   Null   | Ilpwdepts. Count <   1 )
{
Return ;
}
Foreach (Pw_department pditem In Ilpwdepts)
{
Treenode tnnewnode =   New Treenode (pditem. departmentname, pditem. departmentno );
// Obtain Level 2 data under Level 1 Data
Ilist < Pw_department > Ilchillddepts = Userpower. getchilddepartment (pwpid, pditem. departmentno );
Tnrootnode. childnodes. Add (tnnewnode );
If (Ilchillddepts =   Null   | Ilchillddepts. Count <   1 )
{
}
Else
{
// Set the parent node to be scalable
Tnnewnode. populateondemand =   True ;
Tnnewnode. Expanded =   False ;
Nodedictionary. Add (pditem. departmentno, ilchillddepts );
}
}
Tvdepartment. nodes. Add (tnrootnode );
}
// Treenodepopulate event, used for hierarchical data retrieval
Protected   Void Tvdepartment_treenodepopulate ( Object Sender, treenodeeventargs E)
{
// Get the node value of the current click
If (Tvdepartment. selectednode ! =   Null )
{
String Svalue = Tvdepartment. selectednode. value;
}
Userpower =   New Userpower ();
String Nodeid = E. node. value;
If (Nodedictionary ! =   Null )
{
If (Nodedictionary. Keys. Contains (nodeid ))
{
If (Nodedictionary [nodeid]. Count > 0 )
{
// Obtain the first node of the current node from the cache
Foreach (Pw_department pditem In Nodedictionary [nodeid])
{
Treenode tnnewnode =   New Treenode (pditem. departmentname, pditem. departmentno );
// Obtains the subnode of the first node of the current node.
Ilist < Pw_department > Ilpwdepts = Userpower. getchilddepartment (pwpid, tnnewnode. value );
// Expansion is not allowed if no subnode exists.
If (Ilpwdepts =   Null   | Ilpwdepts. Count <   1 )
{
Tnnewnode. Expanded =   False ;
Tnnewnode. populateondemand =   False ;
Tnnewnode. selectaction = Treenodeselectaction. Select;
E. node. childnodes. Add (tnnewnode );
}
Else
{
// Set the parent node to be scalable
Tnnewnode. populateondemand =   True ;
Tnnewnode. Expanded =   False ;
Tnnewnode. selectaction = Treenodeselectaction. Select;
E. node. childnodes. Add (tnnewnode );
// Level-store subnodes under the first level of the current node
Nodedictionary. Add (pditem. departmentno, ilpwdepts );
}
}
// Remove expanded nodes
Nodedictionary. Remove (nodeid );
}
}
}
}

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.