2013-07-08 46 views
0

網格沒有顯示任何值,我可以看到打印在代碼隱藏文件中的JSON值。請找JS代碼和下面代碼隱藏列 -JQGRID + URL + GET

$(document).ready(function() { 

     alert("Page Load1"); 
     createUserGrid(); 
     alert("Page Load2"); 

    }); 

    function createUserGrid() { 
     alert("call webservice"); 
     $("#tblJQGrid").jqGrid({ 
      url: '/POWeb.asmx/GetPOCores?strPoNo=411101', 
      datatype: 'json', 
      mtype: 'GET', 
      loadonce: true, 
      gridview: true, 
      autoencode: true, 
      height:"auto", 
      colNames: ['Number', 'Name', 'Notes'], 
      colModel: [ 
       { name: 'id', index: 'id', width: 60, editable: true, editoptions: { readonly: true }, sorttype: "string" }, 
       { name: 'name', index: 'name', width: 100, sorttype: "string", editable: true }, 
       { name: 'note', index: 'note', width: 150, sortable: false, editable: true } 
      ], 
      jsonReader: { 
       repeatitems: false, 
       root: function (obj) { return obj.d; } 
      }, 
      ajaxGridOptions: { contentType: "application/json; charset=utf-8" }, 

      rowNum: 1, 
      rowList: [1, 2, 3], 
      pager: '#divPager', 
      editurl: 'SaveEdit.aspx', 
      caption: "Users Data", 
      serializeGridData: function (postData) { 
       return JSON.stringify(postData); 
      } 
     }); 

    } 
</script> 

下面是我的代碼隱藏文件

namespace MenuCheck 
{ 
/// <summary> 
/// Summary description for POWeb 
/// </summary> 
[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 
public class POWeb : System.Web.Services.WebService 
{ 

    [WebMethod] 
    public string HelloWorld() 
    { 
     return "Hello World"; 
    } 

    [WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 

    public List<POItems> GetPOCores(string strPoNo) 
    { 
     //string strPoNo = "411101"; 
     System.Diagnostics.Debug.WriteLine(" In Web Method"); 

     List<POItems> result = null; 
     DB dbobj = new DB(); 
     result = dbobj.getPOItems(strPoNo, 0); 

     return result; 
    } 
} 

回答

1

ASMX模型允許返回一個對象序列化爲XML或JSON。 .NET框架爲您做序列化。所以手動撥打JavaScriptSerializer.Serialize是錯誤的。方法GetPOCores應該返回表示所需數據而不是字符串的對象。

所以,你應該做的是類聲明一樣

public class MyItem { 
    public int id { get; set; } 
    public string name { get; set; } 
    public string note { get; set; } 
} 

(確切類型的名稱可以取決於你的數據)。您可以更改Web方法GetPOCores的簽名

public List<MyItem> GetPOCores(string strPoNo) 

,並調整其代碼返回List<MyItem>無需任何手動序列化。

您應該添加loadonce: true選項,因爲您沒有在服務器端實施數據分頁。我建議您另外添加gridview: true, autoencode: true選項並考慮刪除width選項並使用height: "auto"。如果網格寬度將爲colModel所有列的width值的總和。此外,您將需要在jqGrid的以下jsonReader選項:

jsonReader: { 
    repeatitems: false, 
    root: function (obj) { return obj.d; } 
} 

添加

ajaxGridOptions: { contentType: "application/json; charset=utf-8" } 

serializeGridData: function (postData) { 
    return JSON.stringify(postData); 
} 
+0

奧列格感謝您的回覆。我更新了我的代碼。 Web方法未被調用,jgrid未加載。請讓我知道我在代碼中的錯誤。 –