asp.net treeview implementation of infinite-level tree Implementation Code _ Practical skills

Source: Internet
Author: User

First look at the effect chart:

First look at the database table design, the data table mainly includes the Id,name,parentid three items, where the ID is the primary key, parentid the corresponding node's parent node:


Method One: Iterate through the data recursively, and add the nodes to the TreeView individually.
1. First, the database connection and data reading, and the root node first added into the TreeView, and Using recursive Gettreeview () to achieve data traversal and add:

Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
TreeNode nodecategory;
Connection conn = new connection ();
list<category> Category = Conn.getcategory ();
stack<category> storecategory = new stack<category> ();
Storecategory.push (Category[0]);
Nodecategory = new TreeNode (category[0). Name.trim (), category[0]. Id.trim ());
TREEVIEW1.NODES.ADD (nodecategory);
Gettreeview (storecategory, category, nodecategory);
}
}

2. Data traversal of recursive functions, relatively simple to say more.
Copy Code code as follows:

public void Gettreeview (stack<category> categorystack,list<category> categorylist,treenode e)
{
Category tmp;
if (categorystack.count>0)
{
Tmp=categorystack.pop ();
for (int i=0;i<categorylist.count;i++)
if (categorylist[i). Parentid.trim () ==tmp. Id.trim ())
{
Categorystack.push (Categorylist[i]);
TreeNode childnote = new TreeNode (categorylist[i). Name.trim (), Categorylist[i]. Id.trim ());
E.childnodes.add (Childnote);
Gettreeview (Categorystack, CategoryList, childnote);
}
}
}

Method Two: Treeview1_treenodepopulate (object sender, TreeNodeEventArgs e) event response to read child nodes one by one.
1. The first step is consistent with the first step of the previous method, except that the node is set to not expand.
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
TreeNode nodecategory;
Connection conn = new connection ();
list<category> Category = Conn.getcategory ();
Nodecategory = new TreeNode (category[0). Name.trim (), category[0]. Id.trim ());
Nodecategory.populateondemand = true;
Nodecategory.collapse ();
Nodecategory.navigateurl = "http://blog.csdn.net/longlongago2000";
Nodecategory.target = "_blank";
TREEVIEW1.NODES.ADD (nodecategory);
}
}

2. Rewrite treeview1_treenodepopulate (), according to the click of the mouse to get the ID of the node, and then according to the ID of the data read, the Sub node under its read out.
Copy Code code as follows:

protected void Treeview1_treenodepopulate (object sender, TreeNodeEventArgs e)
{
int CategoryID = Int32.Parse (E.node.value);
Connection conn = new connection ();
list<category> Category = Conn.getcategory ();
foreach (Category tmp in Category)
{
if (categoryid.tostring () = = tmp. Parentid.trim ())
{
TreeNode childnote = new TreeNode (tmp. Name.trim (), TMP. Id.trim ());
foreach (Category cate in Category)
{
if (TMP). Id.trim () = = Cate. Parentid.trim ())
{
Childnote.populateondemand = true;
Childnote.collapse ();
Break
}
Else
Childnote.expand ();
}
Childnote.navigateurl = "http://blog.csdn.net/longlongago2000";
Childnote.target = "_blank";
E.node.childnodes.add (Childnote);
}
}

Both of the above methods can achieve an infinite level of classification, but the first method is obviously better, the second method can not achieve full expansion, and the first one can.

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.