Ideas:
1. Load the level-2 Directory of the root node and the root node;
2. Use the expand event to obtain the selected node ID, and use the getnodefromindex () method to obtain the treenode;
3. traverse the child nodes of the obtained treenode node and assign values. In this way, if a node is extended, the content of the child node of the node will be filled in,
It is ensured that the child nodes of the nodes seen in the Treeview control have been filled, and the layer-by-layer expand goes down until there are no child nodes.
// Load
Public void loading ()
{
Bindtree ("0 ");
Expandedorcollapse (null );
}
/*----------------------------------------------------*/
// Expand event
/*----------------------------------------------------*/
Private void treeview1_expand (Object sender, Microsoft. Web. UI. webcontrols. treeviewclickeventargs E)
{
Treenode Tn = treeview1.getnodefromindex (E. node );
Foreach (treenode nd in TN. nodes)
{
Datatable DT;
String strsql = "select ID, name from tablename where parentid =" + nd. type;
Dt = new CC. runsql (strsql );
Treenode tnode;
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Tnode = new treenode ();
Tnode. type = DT. Rows [I] ["ID"]. tostring ();
Tnode. Text = DT. Rows [I] ["name"]. tostring ();
Tn. nodes. Add (tnode );
}
}
}
//
// Bind data
//
Private datatable getdata (string PID)
{
String strsql = "select * From tablename where pid =" + PID;
Return new CC. runsql (strsql). Table [0];
}
// Bind the tree
Private void bindtree (string PID)
{
Datatable dt = getdata (PID );
If (Dt. Rows. Count> 0)
{
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Treenode node = new treenode ();
Node. Text = "name ";
Node. value = "ID ";
This. Treeview. nodes. Add (node );
}
}
}
// Traverse the child nodes under the root node
Private void bindnode (treenode Tn)
{
Datatable dt = getdata (TN. value );
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Treenode node = new treenode ();
Node. Text = DT. Rows [I] ["name"]. tostring ();
Node. value = DT. Rows [I] ["ID"]. tostring ();
Tn. childnodes. Add (node );
// Bindnode (node );
}
}
//
// Selected node change event
//
Public void eventselectedindexchange (Object OBJ, eventargs E)
{
Expandedorcollapse (E. node );
}
//
// Solve the problem of expand and close
//
Public void expandedorcollapse (treenode Tn)
{
This. Treeview. Expanded = false;
If (TN! = NULL)
{
Tn. Expanded = true;
}
Else
{
This. Treeview. nodes [0]. Expanded = true;
}
}