[Original] recursive problem of Treeview! --- FAQ
Problem description:
Database Structure
Table Name: product_class
Product_class_id name parent depth
1 aa 0 1
2 BB 0 1
3 aa1 1 2
4 Aa2 1 2
5 bb1 2 2
6 BB2 2 2
Table Name: product_list
Product_list_id product_class_id name
1 3 aa1product
2 4 aa2product
3 5 bb1product
4 6 bb2product
I want to use the Treeview control to implement the following functions, listing all category names of depth = 1 at the beginning
Click "+" to list the categories of the next level, and so on until the last level. In the last layer, list the product names belonging to this category, and there is a check box before each product name. If it is selected, its value is its id value?
Private Void Page_load ( Object Sender, system. eventargs E)
{
Treeview1.attributes. Add ("oncheck", "getv ();");
// Associate two tables
String Selectmember_name = " Select DBO. product_class. *, DBO. product_list.name as pname from DBO. product_class left Outer Join DBO. product_list on DBO. product_class.product_class_id = DBO. product_list.product_class_id " ;
Sqlconnection myconnection = New Sqlconnection (system. configuration. configurationsettings. receivettings [ " Connectionstring2 " ]);
Myconnection. open ();
Sqlcommand cmd1 = New Sqlcommand (selectmember_name, myconnection );
Sqldataadapter ADP = New Sqldataadapter (selectmember_name, myconnection );
Dataset DS = New Dataset ();
ADP. Fill (DS );
This . Viewstate [ " DS " ] = DS;
Addtree ( 0 , (Treenode) Null );
}
Public Void Addtree ( Int Parent, treenode pnode)
{
Dataset DS = (Dataset) This . Viewstate [ " DS " ];
Dataview dvtree = New Dataview (Ds. Tables [ 0 ]);
// Filter the parentid to obtain all the current child nodes.
Dvtree. rowfilter = " [Parent] = " + Parent;
If (Dvtree. Count = 0 ) // Last layer node
{
Dataview dv = New Dataview (Ds. Tables [ 0 ]);
DV. rowfilter = " [Product_class_id] = " + Parent;
Foreach (Datarowview row In DV)
{
Treenode Node = New Treenode ();
Node. checkbox = true;
Node. ID = row ["product_class_id"]. tostring ();
Node. Text = Row [ " Pname " ]. Tostring ();
Pnode. nodes. Add (node );
}
Return ;
}
Foreach (Datarowview row In Dvtree)
{
Treenode Node = New Treenode ();
If (Pnode = Null )
{ // Add Root Node
Node. Text = Row [ " Name " ]. Tostring ();
Treeview1.nodes. Add (node );
Node. Expanded = True ;
Addtree (int32.parse (row [ " Product_class_id " ]. Tostring (), node ); // Recurrence
}
Else
{ // Add the subnode of the current node
Node. Text = Row [ " Name " ]. Tostring ();
Pnode. nodes. Add (node );
Node. Expanded = False ;
Addtree (int32.parse (row [ " Product_class_id " ]. Tostring (), node ); // Recurrence
}
}
}
This is to view the node ID
< Script Language = Javascript >
Function Getv ()
{
VaR Pnode = Treeview1.gettreenode (treeview1.clickednodeindex)
Alert (pnode. getattribute ( " ID " ));
}
</ Script >