Dynamically generate an infinite tree with TreeView control

xiaoxiao2021-03-06  50

Project needs, there is a tree menu requires dynamic generation, Lenovo TreeView control, decided to use TreeView to implement

First of all, I noticed that TreeView control has a property Treenodesrc

This property can specify an XML file in a fixed format.

XML Version = "1.0" encoding = "UTF-8"

?>

<

Treenodes

>

<

TREENODE

TEXT

= "aaaaaaaa"

Checkbox

= "True"

>

TREENODE

>

<

TREENODE

TEXT

= "bbbbbbbb"

Checkbox

= "True"

>

TREENODE

>

<

TREENODE

TEXT

= "cccccccccc"

Expanded

= "True"

Checkbox

= "True"

>

<

TREENODE

TEXT

= "DDDDDDDDD"

Checkbox

= "True"

>

TREENODE

>

<

TREENODE

TEXT

= "eeeeeeeee"

Checkbox

= "True"

>

TREENODE

>

TREENODE

>

<

TREENODE

TEXT

= "ffffffffffffffff"

Checkbox

= "True"

>

TREENODE

>

Treenodes

>

So I want to read the files in the database to write into an XML file, and then bind it.

Since it is an unlimited level menu, it must be recruited to achieve, so I wrote a recursive algorithm as follows.

Private

Void

Createxml (XmlDocument ObjroToToC, Xmlelement Objrootelem,

int

Belong)

{// get Dataset here's DataSet specific acquisition method I omitted DataSet DS = New Dataset (); Foreach (DataRow Dr in Ds.Tables [0] .ROWS) {// Create ChildNode Treenode Xmlelement ObjXMLEM = Objxmldoc.createElement ( "TREENODE"); objRootElem.AppendChild (objXmlElem); // Create Attributes text XmlAttribute objXmlAttText = objXMLDoc.CreateAttribute ( "text"); objXmlAttText.Value = dr [ "text"] ToString () Trim ();.. objXmlElem. setAttributeNode (objXmlAttText); // Create Attributes CheckBox XmlAttribute objXmlAttCB = objXMLDoc.CreateAttribute ( "CheckBox"); objXmlAttCB.Value = "True"; objXmlElem.SetAttributeNode (objXmlAttCB); int id = int.Parse (dr [ "id"] .Tostring (). Trim ()); CreateXML (Objxmldoc, ObjxmLem, ID);}} then use the following method to implement binding

Private

Void

Bindxmltree ()

{// Create Xml File XmlDocument objXmlDoc = new XmlDocument (); // Insert Xml Declaration XmlDeclaration objXmlDeclare = objXmlDoc.CreateXmlDeclaration ( "1.0", "UTF-8", "yes"); objXmlDoc.InsertBefore (objXmlDeclare, objXmlDoc.DocumentElement ); XmlElement objRootElem = objXmlDoc.CreateElement ( "TREENODES"); objXmlDoc.AppendChild (objRootElem); CreateXml (objXmlDoc, objRootElem, 0); objXmlDoc.Save ( "E: //TreeMenu.xml"); TreeView2.TreeNodeSrc = " TreeMenu.xml ";

I stopped until the last two lines, because the problem occurred that the generated objxmldoc file didn't know what to use to bind the Treenodesrc I tried to try the direct treeview2.treenodesrc = objxmldoc; but TreenodesRC is String data, no way there is no way Nothing method can bind directly to this objxmldoc file? So I changed a way to implement TreeView has a node control called Treenode, directly putting data on Treenode on Treenode.

Void

CreateTree

int

Belong, Microsoft.Web.ui.WebControls.treenode rootnode)

{DataSet DS = New Dataset (); Foreach (DATAROW DR IN DS.TABLES [0] .ROWS) {Microsoft.Web.ui.WebControls.treenode Treenode = New Microsoft.Web.ui.WebControls.treenode (); TREENODE. TEXT = DR ["text"]. TREENODE.CHECKBOX = true; rotnode.Nodes.add (Treenode); int ID = int.parse (DR ["ID" ] .ToString (). Trim ()); CreateTree (ID, TREENODE);}}

Use the same recursive ideas, and I found that this is more simple, many of the Treenode itself is very convenient to modify

If you have to use XML to add every property, trouble

Triggering this method in PageLoad

Private

Void

Page_load

Object

Sender, System.EventArgs E)

{Microsoft.Web.UI.WebControls.TreeNode rootnode = new Microsoft.Web.UI.WebControls.TreeNode (); rootnode.Expanded = true; TreeView2.Nodes.Add (rootnode); CreateTree (0, rootnode);}

Note that this is here to achieve recursion, you have to add an empty root node rootnode

This will make it roughly

In addition, the data format in the database is like this.

-----------------------------------------

ID text belong

1 aaaa 0

2 bbbb 0

3 CCCC 0

4 DDDD 1

5 Eeee 2

转载请注明原文地址:https://www.9cbs.com/read-80335.html

New Post(0)