What i want to do is, i have a web service which returns number of products (from sample Northwind database) in json format and i want to bind returned row to my jqGrid. I tried JQGrid - Cannot call ASP.NET WebMethod but can with Ajax but it didn't work for me with web service.
Then just for a trial I wrote [Web Method] in same aspx page on which i am creating jqGrid then it did work for mtype:"POST". Below is my code.
Working Code Without Web Service
Code in 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>
Code in .cs file
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<Products> GetProducts()
{
Products product = new Products();
return product.GetProducts();
}
This is working but if i do not want to write web method in the form. I have added web reference to my application and want to use data from web service. I tried below in jqGrid but did not help me.
$(function () {
jQuery("#jqGridXML").jqGrid({
url: "http://localhost:49493/jqGrid_HttpHandlers/WebService.asmx/GetProducts",
mtype: "POST",
Rest part is same.
Code in Web Service
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();
}
}
Could you please help me to understand things. I just started working on jqGrid.
Your help is really appreciated.
EDIT
Yes you are correct Dave. I only uncommented below statement and removed static and ran the project without any modification and it did work perfectly.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
Thanks, but my next doubt, how can i use jqGrids navigator functionality, like Paging, Sorting those things with the same code. I have used this,
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",
but I really do not have idea where exactly this need to be used in my code. Could you please redirect me to correct solution.