2015-01-30 2 views
0

Я пытаюсь вызвать веб-метод из jsTree, но не смог его назвать. кто-то может помочь мне решить эту проблему.Веб-метод не запускается jsTree

моя функция jsTree является: -

$('#tree').jstree({ 
    "json_data": { 
     "ajax": { 
      "type": "POST", 
      "dataType": "json", 
      "async": true, 
      "contentType": "application/json;", 
      "opts": { 
       "method": "POST", 
       "url": "../../SurveyReport/Metrics.aspx/GetAllNodes11" 
      }, 
      "url": "../../SurveyReport/Metrics.aspx/GetAllNodes11", 
      "data": function (node) { 
       if (node == -1) { 
        return '{ "operation" : "get_children", "id" : -1 }'; 
       } 
       else { 
        //get the children for this node 
        return '{ "operation" : "get_children", "id" : ' + $(node).attr("id") + ' }'; 
       } 
      }, 
      "success": function (retval) { 
       alert('Success') 
       return retval.d; 
      }, 
      "error": function (r) { 
       alert(r.attr); 
       alert('error'); 
      } 
     } 
    }, 
    "plugins": ["themes", "json_data"] 
}); 

и веб-метод и файл данных: -

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public static List<G_JSTree> GetAllNodes11(string id) 
{ 
    if (id != "-1") //-1 means initial load else async loading of children 
    { 
     if (id == "10") 
      //Add 3 children to parent node with id=10. 
      return AddChildNodes(10, 3, "xxxx"); 
     else 
      return new List<G_JSTree>(); 
    } 
    List<G_JSTree> G_JSTreeArray = new List<G_JSTree>(); 

    //Creating the JsTree data 
    //In live scenarios this will come from db or Web Service 
    //Add 5 root nodes 
    G_JSTreeArray.AddRange(AddChildNodes(0, 5, "")); 

    //Add 4 children to 3rd root node 
    //The third node has id=30 
    //The child nodes will have ids like 301,302,303,304 
    G_JSTreeArray[3].children = (AddChildNodes(30, 4, G_JSTreeArray[3].data)).ToArray(); 

    //Add 5 children to level1 Node at id=302 
    G_JSTreeArray[3].children[1].children = (AddChildNodes(302, 4, G_JSTreeArray[3].children[1].data)).ToArray(); 

    return G_JSTreeArray; 
} 


private static List<G_JSTree> AddChildNodes(int _ParentID, int NumOfChildren, string ParentName) 
{ 
    List<G_JSTree> G_JSTreeArray = new List<G_JSTree>(); 
    int n = 10; 
    for (int i = 0; i < NumOfChildren; i++) 
    { 
     int CurrChildId = (_ParentID == 0) ? n : ((_ParentID * 10) + i); 
     G_JSTree _G_JSTree = new G_JSTree(); 
     _G_JSTree.data = (_ParentID == 0) ? "root" + "-Child" + i.ToString() : ParentName + CurrChildId.ToString() + i.ToString(); 
     _G_JSTree.state = "closed"; //For async to work 
     _G_JSTree.IdServerUse = CurrChildId; 
     _G_JSTree.children = null; 
     _G_JSTree.attr = new G_JsTreeAttribute { id = CurrChildId.ToString(), selected = false }; 
     G_JSTreeArray.Add(_G_JSTree); 
     n = n + 10; 
    } 
    return G_JSTreeArray; 
} 

public class G_JSTree 
{ 
    public G_JsTreeAttribute attr; 
    public G_JSTree[] children; 
    public string data 
    { 
     get; 
     set; 
    } 
    public int IdServerUse 
    { 
     get; 
     set; 
    } 
    public string icons 
    { 
     get; 
     set; 
    } 
    public string state 
    { 
     get; 
     set; 
    } 
} 

public class G_JsTreeAttribute 
{ 
    public string id; 
    public bool selected; 
} 

}

Я хочу, чтобы загрузить дерево в асинхронном моды из webmethod на странице asp.net.

Заранее спасибо.

+0

Что конкретная проблема с кодом у вас есть прямо сейчас? –

+0

спасибо за ваш ответ, webmethod, который написан на url, не вызывается вышеуказанным кодом. –

ответ

0

Я использую этот код успешно К добавить полный справочник:

[System.Web.Services.WebMethod] 
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] //ResponseFormat.Json)] 
     public static List<GG_JSTree> GetAllNodes11(string id) 
     {......}