Recently I made a system, but loading a large volume of data slows down page loading ,. net Treeview controls are slow when you use recursive binding when there are hundreds of data records. It is really a pain.
The idea at the beginning was Ajax, which looked pretty good, but encountered many problems, probably because it was not familiar with Ajax.
After a long time, I found that the native Treeview can be done. Here I need dynamic backend binding and use the treenodeexpanded event and node. populateondemand = true; attribute.
The foreground directly displays the Treeview control and starts binding to the background. The following is an example:
Private void initree ()
{
Try
{
Treedata. nodes. Clear ();
Datarow [] rowlist = Ds. Tables [0]. Select ("parentid = 0 ");
Foreach (datarow row in rowlist)
{
Treenode node = new treenode ();
Node. Text = row ["cname"]. tostring ();
Node. value = row ["dataid"]. tostring ();
// Determine whether a node has a subnode and set whether the node is dynamically filled
If (pubfuncs. checkdatatablerows (Ds. Tables [0]. Select ("parentid =" + row ["dataid"]. tostring ())))
{
Node. populateondemand = true;
}
Treedata. nodes. Add (node );
}
// By default, only one layer is expanded and the treenodeexpanded event is automatically called.
Treedata. expanddepth = 1;
}
Catch (exception E)
{
Throw E;
}
}
/// <Summary>
/// Create a subnode of the tree
/// </Summary>
Public void createsubtree (treenode parentnode, string parentid)
{
// Clear original data of the parent node
Parentnode. childnodes. Clear ();
Datarow [] rowlist = Ds. Tables [0]. Select ("parentid =" + parentid );
Foreach (datarow row in rowlist)
{
Treenode node = new treenode ();
Node. Text = row ["cname"]. tostring ();
Node. value = row ["dataid"]. tostring ();
// Determine whether a node has a subnode and set whether the node is dynamically filled
If (pubfuncs. checkdatatablerows (Ds. Tables [0]. Select ("parentid =" + row ["dataid"]. tostring ())))
{
Node. populateondemand = true;
}
Parentnode. childnodes. Add (node );
}
}
Protected void treedata_treenodeexpanded (Object sender, treenodeeventargs E)
{
// Determine whether a child node exists in the expanded node.
// You can consider not to add a judgment. It is reloaded every time it is expanded, and the efficiency will be relatively low.
If (E. node. childnodes. Count <= 0)
{
Createsubtree (E. node, E. node. value. Trim ());
}
}
Of course, the effect may not be as good as the dynamic display of Ajax. However, the display speed of the control is much faster. I have been busy for a long time and have made great achievements.