【问题标题】:Can you use jqGrid within ASP.net using a webservice and javascript?您可以使用 web 服务和 javascript 在 ASP.net 中使用 jqGrid 吗?
【发布时间】:2011-03-10 06:09:49
【问题描述】:

jqGrid 一直在踢我(以及本网站上的其他人)。使用 addJSONData 方法时,我似乎无法从 web 服务获取 JSON 数据以加载到 jqGrid。

有谁知道这是否可行?我在 ASP.NET 3.5 中使用的不是 MVC,只是一个普通的 WebProject 网络服务。

我正在使用最新版本的 jqGrid 3.5。

我不知道该怎么办。我目前正在尝试仅加载 1 行,我在我的 WS 中返回一个字符串,如下所示:

"Page:1,Total:1,Records:1,Rows:[name: Jeff V title: Programmer]"

然后将其传递到我的 javascript 中: {"d":"Page:1,Total:1,Records:1,Rows:[name: Jeff Vaccaro title: Programmer]"}

我的 jQuery 代码如下:

    jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        datatype: processrequest,
        mtype: 'POST',
        colNames: ['Name', 'Title'],
        colModel: [
      { name: 'name', index: 'name', width: 55 },
      { name: 'title', index: 'title', width: 90 }
      ],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [10, 20, 30],
        sortname: 'id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: 'themes/basic/images',
        caption: 'My first grid'
    });
});


function processrequest(postdata) {
    $(".loading").show();
    $.ajax({
        type: "POST",
        data: "{}",
        datatype: "clientside",
        url: "../webServices/myTestWS.asmx/testMethod",
        contentType: "application/json; charset-utf-8",
        complete: function (jsondata, stat) {
            if (stat == "success") {
                jQuery("#list")[0].addJSONData(eval("(" + jsondata.responseText + ")"));
                $(".loading").hide();
            } else {
                $(".loading").hide();
                alert("Error with AJAX callback");
            }
        }
    });
}

我已经尝试了 addJSONData 代码的各种不同变体。有谁知道这是否可行?

感谢任何帮助!

谢谢

【问题讨论】:

    标签: asp.net web-services jqgrid jqgrid-asp.net


    【解决方案1】:

    首先,您的 Web 服务应该返回一个具有 pagetotalrecordsrows 等属性的类实例。如果 web 方法具有属性[ScriptMethod (ResponseFormat = ResponseFormat.Json)],则类实例将正确转换为 JSON 数据。

    您可以使用ajaxGridOptions: { contentType: 'application/json; charset=utf-8' } 参数和jsonReader(参见http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data)在jqGrid 中加载数据。来自Jqgrid 3.7 does not show rows in internet explorerBest way to change jqGrid rowNum from ALL to -1 before pass to a web service 的信息可能对您有所帮助。

    最后,jqGrid 3.5 版不再支持 imgpath

    【讨论】:

    • OK... 在更改我的 WS 以拉回一个非常简单的测试对象后,该对象返回总数、页面、记录和行(列表数据类型),然后在 WS 中使用以下内容: _ _ 我还添加了: jsonReader: { root: "rows", page: "page", total: "total", records: "records", repeatitems: false, id: "ID" // 包含 PK 的列的索引 },到我的 JavaScript 文件。毕竟它终于奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 2010-09-07
    相关资源
    最近更新 更多