Turn from: http://www.iteye.com/topic/1122691
Data tree structure output based on recursive algorithm in C # traversal tree and Ajax read data
Data table field: ID,NAME,PID (parent ID)
JS Code
JavaScript code
<script language= "javascript" type= "Text/javascript" > var htmlstr=new Array ();
$ (function () {loadnode (0);
}) function Loadnode (PID) {var vdtbl=_default.getchildren (PID). value; if (Vdtbl!=null) {for (var i = 0; i < vDtbl.Rows.length i++) {var NodeID =
vdtbl.rows[i]["id"];
var nodename=vdtbl.rows[i]["Name"];
var pid=vdtbl.rows[i]["PID"];
Htmlstr.push ("<ul>");
Htmlstr.push ("<li id= '" +nodeid+ "' >"); Htmlstr.push (nodename); Htmlstr.push ("</li>");
if (vdtbl.rows.length!=0)//Determines whether the NodeID exists a subset {Loadnode (NodeID);
} htmlstr.push ("</ul>");
}//end for Var htmlcode= "";
Htmlcode=htmlstr.join ("");
$ ("#loadNode"). HTML (Htmlcode); }} </script>
Server-side C # code
C # code
Public DataTable getchildren (int id)
{
//string Strcon = system.web.configuration.webconfigurationmanager.connectionstrings["Siteconnectionstring"]. ConnectionString;
String Strcon = "Data source=.;i ntegrated security=sspi;initial catalog=test ";
String Strcon = "server=.; Database=test;integrated Security=sspi ";
using (SqlConnection conn = new SqlConnection (Strcon))
{
using (SqlCommand cmd = conn. CreateCommand ())
{
cmd.commandtext = "Select ID, name,pid from tab_tree where pid=" + ID;
Conn. Open ();
Cmd.commandtype = CommandType.Text;
SqlDataAdapter DbA = new SqlDataAdapter (cmd);
DataSet ds = new DataSet ();
Dba.fill (DS);
Conn. Close ();
Return DS. Tables[0];}}
Attachment for the test project I wrote
Traverse tree. RAR (90.5 KB) Download number of times: 100
Size: 5.9 KB