Building tree structure from categories and sub categories and its sub categories
<p>I need to develop a tree structure between categories and subcategories.  I'm using a handler to generate the relation.</p>

<p>For example my SQL table contains</p>

<pre>GlobalID for top-level parents 1000

Id      CategoryName          ParentId
 1        toyota               1000
 2        ford                 1000  
 5        prius                 1
 6        prius SP              5
 7        prius SP blue         6
 8        focus                 2
 9        mondeo                2 
 10       mustang               2
 11       focus EX              8
 ...
</pre>

<p>So the categories looks like</p>

<pre>
toyota 
   prius
     prius SP
       prius PS blue
  yaris
  auris

ford
  focus
    focus EX
  mondeo
  mustang
</pre>

<p>etc</p>

<hr>

<p>To  do this Im using a recursive program whih will loop thorugh its child categories.</p>

<p>But How can I add the tree structure and send it back to the aspx page.</p>

<p>The code I'm using is </p>

<pre><code>// To get all the parent categories

List&lt;Category&gt; objChildren = new List&lt;Category&gt;();

public List&lt;Category&gt; GetCategories(int categoryId)
{   
  IQueryable&lt;Category&gt; cats = from ca in db.Categories
                              where ca.ParentId == categoryId &amp;&amp; ca.ParentId != nonPageId
                              select ca;

  return cats.ToList();
}

List&lt;Category&gt; parentCategories = GetCategories(1000);
foreach(Category _category in parentCategories)
{
  GetChildCats(_category);
}

public void GetChildCats(Category cat)
{
  objChildren.Add(cat);

  List&lt;Category&gt; cats = GetCategories(cat.CatId); 

  if (cats.Count != 0)
  { 
    foreach (Category cate in cats)
    {              
      GetChildCats(cate);               
    }
  }
}
</code></pre>

<p>How to return a JSON tree structure to the aspx page like this </p>

<pre><code>&lt;/pre&gt;
So the categories looks like
&lt;pre&gt;
[0] =&gt; toyota 
        [0] =&gt; prius
                [0] =&gt; prius SP
                         [0] =&gt; prius PS blue
        [1] =&gt; yaris
        [2] =&gt; auris

[1] =&gt; ford
        [0] =&gt; focus
                 [0] =&gt; focus EX
        [1] =&gt; mondeo
        [2] =&gt; mustang
&lt;/pre&gt;
</code></pre>
