使用json在asp.net中调用递归函数

时间:2013-09-12 18:07:30

标签: jquery asp.net json

我是json和他的新手...... 我正在尝试将递归表绑定到嵌套的ul元素。 我决定将父项绑定到li元素,因此当用户单击该项时,我想加载其子项。 它适用于第一批孩子,但是当我想加载内部级别时,虽然它通过我的方法并获取适当的数据,仍然显示当前级别。当我检查我发现它调用内部方法两次...

这是我正在调用的方法,第一次使用ID = 0,之后使用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>

我在这里做错了什么?!

1 个答案:

答案 0 :(得分:0)

当您有onclick时,事件将冒泡并调用父方法,因此您需要做的是使用以下命令停止传播:

event.stopPropagation();