Infinite level traversal tree (recursive)

Source: Internet
Author: User
Tags dba
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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.