[Asp.net] Treeview (1) (one-time recursion of all nodes)

Source: Internet
Author: User

Database Table
========================================================== ======

Int key int nvarchar (100) nvarchar (100) nvarchar (100) nvarchar (100)
Node_id parent_id node_name address image_url select_image_url
1 0 node_1 http: // **. gif *. gif
2 1 node_1_1 http: // **. gif *. gif
3 2 node_1_1_1 http: // **. gif *. gif
4 0 node_2 http: // **. gif *. gif
5 4 node_2_1 http: // **. gif *. gif
6 5 node_2_1_1 http: // **. gif *. gif
7 0 node_3 http: // ** GIF *. gif
8 7 node_3_1 http: // **. gif *. gif

========================================================== ======

CodeProgram:

========================================================== ======

using system;
using system. collections;
using system. componentmodel;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;
using components;
using Microsoft. web. UI. webcontrols;

namespace register
{< br> ///


/// abstract description of webform1.
//
public class webform1: system. web. UI. page
{< br> protected Microsoft. web. UI. webcontrols. treeview tvlist;
private sqlcontrol objsqlcontrol;
private datatable objdatatable;
private void page_load (Object sender, system. eventargs e)
{< br> // place the user code here to initialize the page
If (! Page. ispostback)
{< br> objsqlcontrol = new sqlcontrol ();
treenode node = new treenode ();
objdatatable = objsqlcontrol. selectdata (); // obtain all the data to obtain the datatable
This. createtree (tvlist. nodes, "0"); // create a node
}< BR >}

Private void createtree (treenodecollection node, string parent_id)
{
Dataview dvlist = new dataview (this. objdatatable );
Dvlist. rowfilter = "parent_id = '" + parent_id + "'"; // filter the parent node
Treenode nodetemp;
Foreach (datarowview DV in dvlist)
{
Nodetemp = new treenode ();
Nodetemp. ID = DV ["node_id"]. tostring (); // node ID
Nodetemp. Text = DV ["node_name"]. tostring (); // node name
Nodetemp. navigateurl = DV ["Address"]. tostring (); // node link address
Nodetemp. imageurl = DV ["image"]. tostring (); // node image (not expanded)
Nodetemp. selectedimageurl = DV ["image_ex"]. tostring (); // node image (expand)
Nodetemp. Target = "_ parent"; // node Link Target

Node. Add (nodetemp); // Add a node
This. createtree (nodetemp. nodes, nodetemp. ID); // Recursion
}
}

========================================================== ==========

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.