栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在后端使用ASP.NET + JSON使jqGrid正常工作?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在后端使用ASP.NET + JSON使jqGrid正常工作?

这是一个简单的例子…

您需要使用https://github.com/douglascrockford/JSON-
js/blob/master/json2.js
才能正常工作…

当然还有常用的jquery文件。

将此粘贴到Web服务

// The lower case properties here are required to be lower case// I cant find a way to rename them when they are serialized to JSON// XmlElement("yournamehere") does not work for JSON :(public class JQGrid{    public class Row    {        public int id { get; set; }        public List<string> cell { get; set; }        public Row()        { cell = new List<string>();        }    }    public int page { get; set; }    public int total { get; set; }    public int records { get; set; }    public List<Row> rows { get; set; }    public JQGrid()    {        rows = new List<Row>();    }}[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][scriptService]public class MyWebService : System.Web.Services.WebService{    [WebMethod(EnableSession = true)]    [scriptMethod(ResponseFormat = ResponseFormat.Json)]        public JQGrid GetJQGrid(int page, int pageSize, string sortIndex, string sortDirection)    {        DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, "udsp_GetMyData",pageIndex, pageSize);        if (ds == null || ds.Tables.Count < 1) throw new Exception("Unable to retrieve data.");        JQGrid jqGrid = new JQGrid();        int i = 1;        foreach (DataRow dataRow in ds.Tables[0].Rows)        { JQGrid.Row row = new JQGrid.Row(); row.id = Convert.ToInt32(dataRow["MyIdColumn"]); row.cell.Add(dataRow["MyIdColumn"].ToString()); row.cell.Add(dataRow["MyColumn"].ToString()); projectGrid.rows.Add(row);        }        jqGrid.page = 1; // Set this when you are actually doing paging... this is just a sample        jqGrid.records = jqGrid.rows.Count;        jqGrid.total = jqGrid.rows.Count;  // Set this to total pages in your result...        return jqGrid;    }}

将此粘贴到您的aspx页面

<script type="text/javascript">function getData(pdata) {    var params = new Object();    params.page = pdata.page;    params.pageSize = pdata.rows;    params.sortIndex = pdata.sidx;    params.sortDirection = pdata.sord;    $.ajax({        type: "POST",        contentType: "application/json; charset=utf-8",        url: "/CLM/CLM.asmx/GetProjectGrid2",        data: JSON.stringify(params),        dataType: "json",        success: function(data, textStatus) { if (textStatus == "success") {     var thegrid = $("#testGrid")[0];     thegrid.addJSonData(data.d); }        },        error: function(data, textStatus) { alert('An error has occured retrieving data!');        }    });}var gridimgpath = '/clm/css/ui-lightness/images';$(document).ready(function() {    $("#testGrid").jqGrid({        datatype: function(pdata) { getData(pdata);        },        colNames: ['My Id Column', 'My Column'],        colModel: [ { name: 'MyIdColumn', index: 'MyIdColumn', width: 150 }, { name: 'My Column', index: 'MyColumn', width: 250 }        ],        rowNum: 10,        rowList: [10, 20, 30],        imgpath: gridimgpath,        pager: jQuery('#pagerdt'),        sortname: 'id',        viewrecords: false,        sortorder: "desc",        caption: "Projects",        cellEdit: false    });});</script>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号