0

我想要做的是,我有一个 Web 服务,它以 json 格式返回产品数量(来自示例 Northwind 数据库),我想将返回的行绑定到我的 jqGrid。我尝试了 JQGrid - 无法调用 ASP.NET WebMethod 但可以使用 Ajax 但它不适用于我使用 Web 服务。

然后只是为了试用,我在创建 jqGrid 的同一个 aspx 页面中编写了 [Web Method],然后它确实适用于 mtype:“POST”。下面是我的代码。

没有 Web 服务的工作代码

ASPX 中的代码:

<script language="javascript" type="text/javascript">
        $(function () {
            jQuery("#jqGridXML").jqGrid({
                url: "jqGridXML.aspx/GetProducts",
                mtype: "POST",
                ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
                datatype: "json",
                serializeGridData: function (postData) {
                    return JSON.stringify(postData);
                },
                jsonReader: {
                root: function (obj) { return obj.d;},
                page:function (obj) {return 1;},
                total: function (obj) {return 1;},
                records:function (obj) { return obj.d.length;},
                id:"0",
                cell:"",
                repeatitems:false
            },
            datatype:"json",
            height:250,
            colName:['ProductId','ProductName','UnitsInStock'],
            colModel:[{name:'ProductId',index:'ProductId',width:100},
            {name:'ProductName',width:100},
            {name:'UnitsInStock',width:100}],
            caption:"Products"
        });
    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table id="jqGridXML"></table>
    <div id="pagerXML"></div>
    </form>
</body>

.cs 文件中的代码

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<Products> GetProducts()
{
    Products product = new Products();
    return product.GetProducts();
}

这是可行的,但如果我不想在表单中编写 web 方法。我已将 Web 引用添加到我的应用程序并希望使用来自 Web 服务的数据。我在 jqGrid 中尝试过,但没有帮助我。

$(function () {
    jQuery("#jqGridXML").jqGrid({
        url: "http://localhost:49493/jqGrid_HttpHandlers/WebService.asmx/GetProducts",
        mtype: "POST",

其余部分相同。

Web 服务中的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Collections.ObjectModel;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public static List<Products> GetProducts()
    {
        Products product =  new Products();
        return product.GetProducts();
    }


}

你能帮我理解一些事情吗?我刚开始研究 jqGrid。

非常感谢您的帮助。

编辑 

是的,你是正确的戴夫。我只取消了以下语句的注释并删除了 static 并在没有任何修改的情况下运行了该项目,它确实运行良好。 

[System.Web.Script.Services.ScriptService]  
public class WebService : System.Web.Services.WebService

谢谢,但我的下一个疑问是,我如何使用 jqGrids 导航器功能,例如分页,用相同的代码对这些东西进行排序。这个我用过   

jsonReader: { 
                root: function (obj) { return obj.d;}, 
                page:function (obj) {return 1;}, 
                total: function (obj) {return 1;}, 
                records:function (obj) { return obj.d.length;}, 
                id:"0", 

  但我真的不知道在我的代码中究竟需要在哪里使用它。您能否将我重定向到正确的解决方案。 

4

1 回答 1

0

您需要取消注释该[ScriptService]属性(并且大多数其他装饰都是不必要的)。此外,该方法不能是静态的。仅当您在 ASPX 代码后面声明方法时才需要这样做

所以,像这样:

[WebService]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
  [WebMethod]
  public List<Products> GetProducts()
  {
    Products product =  new Products();
    return product.GetProducts();
  }
}
于 2012-06-30T19:55:58.190 回答