Ajax is not required. Data is loaded when the Treeview control is expanded.

Source: Internet
Author: User

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.

 

 

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.