The table structure of the table generating the menu in the database
Figure:
Among them, MNU_XH is the serial number of the node, the MNU_name is the text to be displayed, and the mnu_url is the web page to which the node is to be linked, the MNU_FBM is the serial number of the parent node,
Mnu_img is a node graphic, MNU_END is whether it is a finite point, SqlDst.Tables [0] is a record of all nodes to be displayed on the tree.
When Page is first loaded, the first level directory of the FillTree fill the tree is called, the parent node of the first level is "0".
GetNode (this.treeView1, "0"); // Get the current node
this.treeView1.nodes [0] .id = "0"; // Set the ID of the root node
this.treeView1.nodes [0] .text = "admin"; // Set the text displayed by the root node
this.treeView1.nodes [0] .NodeData = "0"; // Set Nadedata of the root node
FillTree (Nodex, "MNU_LB = '1' and MNU_FBM = '0'", "MNU_XH", "MNU_NAME", "MNU_URL", "MNU_IMG", "MNU_LB", "MNU_END");
Where MNU_BL, MNU_FBM is the condition of filtering data from the data source, and Nodex is the current node.
FillTree as a method of filling in a tree, filling the node below the current node TNC.
Private Void FillTree (Treenode TNC, String Strslt, String Strince, String Strimg, String Strower, String Strend)
{
DATAROW [] DRS;
DRS = Sqldst.tables [0] .select (strslt); // // // According to STRSLT, there is conditional filtering
TNC.NODES.CLEAR (); // Remove the subtree
Foreach (DataRow Dr in DRS)
{
Treenode rootnode = new treenode ();
Rootnode.id = GETROWVALUE (DR, STRCODE); / / // as the value of the parent as the ID of the node
Rootnode.text = GetrowValue (DR, STRNAME); //
Rootnode.naviGateURL = GetrowValue (DR, Strurl);
Rootnode.ImageURL = GETROWVALUE (DR, STRIMG);
Rootnode.expandedImageURL = GetrowValue (DR, STRIMG);
Rootnode.selectedImageURL = GetrowValue (DR, STRIMG);
Rootnode.nodedata = GetrowValue (DR, STRGRADE);
TNC.NODES.ADD (RootNode); / / Add a node for TNC
IF (GetrowValue (DR, Strend) == "0")
{
Treenode tmpnode = new Treenode (); // Add a temporary node
TNC.NODES [TNC.NODES.COUNT-1] .NODES.ADD (TmpNode);
}
}
}
// The following is the Expand Event of TreeView, expand the subordinate node.
Private void TreeView1_expand (Object Sender, Microsoft.Web.ui.WebControls.TreeViewClickeventargs E) {
GetNode (THIS.TREEVIEW1, E.NODE);
IF (THIS.TREEVIEW1.ENABLED == false)
Nodex.expanded = false;
Else
{
INT jc = int.parse (Nodex.NodeData.toT7tring ()) 1;
// Fill the tree, MNU_LB (NodeData), MNU_FBM (ID) is condition
FillTree (Nodex, "MNU_LB = '" JC "' AND MNU_FBM = '" NodeID "'", "MNU_XH", "MNU_NAME", "MNU_URL", "MNU_IMG", "MNU_LB", "MNU_END");
}
}
/ / Remove the value corresponding to the database
Private String GetrowValue (DataRow DR, String Scol)
{
IF ((Dr.Table.Columns.contains (SCOL) == false) || (Dr.Isnull (SCOL)))
""; "
Else
{
IF (DR [scol] .gettype (). Tostring () == "System.Boolean")
IF (DR [scol] .toString () == "true")
Return "1";
Else
Return "0";
Else
Return DR [scol] .tostring ();
}
}
/ / Get the current node
Private void getNode (TreeView TV, String XH)
{
String [] ID = xh.split ('.');
Treenode tc = tv.nodes [0];
For (int I = 1; i { Tc = tc.nodes [int.parse (ID [i])]; } Nodex = Tc; // current node NodeID = tc.id; // Current number }