Thursday, 12 September 2013

call recursive function in asp.net using json

call recursive function in asp.net using json

I'm new to json and stuff .. I'm trying to bind a recursive table to a
nested ul element . I decided to bind parent Item into a li element , so
when user click on the item , i want to load its children . it works for
the first bunch of children , but when i want to load inner level ,
although it goes through my method and fetch the appropriate data , still
shows current level . and when i checked i found out it calls the inner
method twice ...
That's the method i'm calling , first time with ID=0 and after that with
currentParentID :
public static IEnumerable GetItemsByParentID(int pID)
{ return from ctg in _ctx.Categories where ctg.CategoryParentID == pID
select ctg; }
MyTreeview.js :
$(document).ready(function () {
$.ajax({
type: "POST",
url: "Default2.aspx/GetItemsByParentID",
data: "{'pID':'0'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$('#MyContainer').setTemplateURL ('MyCtg.htm',null, { filter_data:
false });
$('#MyContainer').processTemplate(msg);
}
});
});
function LoadChildren(value) {
$.ajax({
type: "POST",
url: "Default2.aspx/GetItemsByParentID",
data: "{'pID':'"+value+"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$('#child'+value).setTemplateURL('MyCtg.htm',null, { filter_data:
false });
$('#child' + value).processTemplate(msg);
}
});
}
MyCtg.htm:
<ul>
{#foreach $T.d as post}
<li onclick="LoadChildren({$T.post.CategoryID})">
{$T.post.CategoryName}
<ol id="child{$T.post.CategoryID}"></ol>
</li>
{#/for}
Default.aspx : <div id="MyContainer"> </div>
what am i doing wrong here ?!

No comments:

Post a Comment