Asp.net treeview implements the code of an infinite level tree

Source: Internet
Author: User

Let's take a look:

First, let's look at the design of the database table. The data table mainly includes ID, Name, and ParentID, where ID is the primary key and ParentID corresponds to the parent node of the node:


Method 1: recursively traverse the data and add the nodes to the treeview one by one.
1. perform database connection and Data Reading First, add the root node to the treeview first, and use recursive getTreeView () to traverse and add data:

Copy codeThe Code is 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. recursive functions for data traversal are much simpler.Copy codeThe Code is 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 2: Use the treeviewinclutreenodepopulate (object sender, TreeNodeEventArgs e) Event Response to read the subnodes one by one.
1. The first step is basically the same as the first step of the previous method, but you only need to set the node to not expand.Copy codeThe Code is 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 (), click the mouse to get the node ID, then read the data based on the ID, and read the sub-nodes under it.Copy codeThe Code is as follows: protected void treeviewinclutreenodepopulate (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 );
}
}

The above two methods can achieve unlimited classification, but the first method is obviously better. The second method cannot achieve full expansion, but the first method 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.