hi, it is ok with IHttpHandler,you may try it, but my code have some bug, next page and pre page is bad.
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
context.Response.ContentType = "text/plain";
int page = 1;
if (HttpContext.Current.Request.Form["page"] != null)
{
page = int.Parse(HttpContext.Current.Request.Params["page"].ToString());
}
int rows = 10;
if (HttpContext.Current.Request.Form["rows"] != null)
{
rows = int.Parse(HttpContext.Current.Request.Form["rows"].ToString());
}
string sidx = "Name";
if (HttpContext.Current.Request.Form["sidx"] != null)
{
sidx = HttpContext.Current.Request.Form["sortname"].ToString();
}
string whereCondition = "";
//if (HttpContext.Current.Request.Form["qtype"] != null && HttpContext.Current.Request.Form["query"] != null && HttpContext.Current.Request.Form["query"].ToString() != string.Empty)
//{
// whereCondition = BuildWhereCondition(HttpContext.Current.Request.Form["qtype"].ToString(), HttpContext.Current.Request.Form["query"].ToString());
//}
string sord = "asc";
if (HttpContext.Current.Request.Form["sord"] != null)
{
sord = HttpContext.Current.Request.Form["sord"].ToString();
}
string sortExp = sidx + " " + sord;
int start = ((page - 1) * rows);
DataAccess newDataAccess = new DataAccess();
List<Product> data = newDataAccess.GetAllProducts(whereCondition, sortExp, start, rows);
JavaScriptDateTimeConverter aa = new JavaScriptDateTimeConverter();
string strlist = Newtonsoft.Json.JsonConvert.SerializeObject(data,new JavaScriptDateTimeConverter());
string ttt="";
for (int j = 0; j < HttpContext.Current.Request.Form.Count; j++)
{
ttt += "--" + HttpContext.Current.Request.Form[j];
}
System.IO.File.WriteAllText("d:\\aaab.txt", HttpContext.Current.Request.Form["page"] + "===" + page.ToString() + "-----" + start.ToString() + "---" + sortExp + "-" + rows);
int totalpage = newDataAccess.TotalRows / rows + 1;
string str = "{totalpages: \"" + totalpage.ToString() + "\",";
str +="currpage: \"" + page.ToString() + "\",";
str +="totalrecords: \"" + newDataAccess.TotalRows.ToString() +"\",rows : ";
// one way demo
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]},";
//str += "{id:\"1\", cell:[\"1\", \"sdd\", \"2009-05-06\",\"dfdf\",\"dfdff\",\"78\"]}";
//second way demo
// str += "{Id:6,Name:\"6565\",Description:\"6565\",Unit:\"6565\",UnitPrice:6565,CreateDate:\"2009-6-5\"},";
// str += "{Id:6,Name:\"6565\",Description:\"6565\",Unit:\"6565\",UnitPrice:6565,CreateDate:\"2009-6-5\"}]}";
str += strlist +"}";
context.Response.Write(str);
HTML code is
jQuery(document).ready(function() {
jQuery("#west-grid").jqGrid({
url: 'Handler1.ashx',
datatype: 'json',
type: "POST",
jsonReader: {
root: "rows",
page: "currpage",
total: "totalpages",
records: "totalrecords",
repeatitems: false,
id: "0"
},
colNames: ['Id', 'Name', 'Description', 'Unit', 'UnitPrice', 'CreateDate'],
colModel: [
{ name: 'Id', index: 'Id', width: 55 },
{ name: 'Name', index: 'Name', width: 90, editable: true },
{ name: 'Description', index: 'Description', width: 80, align: 'right', editable: true },
{ name: 'Unit', index: 'Unit', width: 80, align: 'right', editable: true },
{ name: 'UnitPrice', index: 'UnitPrice', width: 80, align: 'right', editable: true },
{ name: 'CreateDate', index: 'CreateDate', width: 150, date: true, datefmt: 'Y-m-d', sortable: false, editable: true}],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30],
sortname: 'id',
sortorder: "desc",
root: "rows",
page: "currpage",
total: "totalpages",
records: "totalrecords",
repeatitems: false,
id: "0",
viewrecords: true,
imgpath: 'themes/basic/images',
caption: 'My first grid'
});
jQuery("#west-grid").navGrid('#Pager', {
edit: true, add: true, del: true, search: true
}).navButtonAdd('#Pager', {
caption: "Add", buttonimg: "fullpath/row_add.gif", onClickButton: function() { alert("Adding Row") }, position: "last"
}).navButtonAdd('#Pager', {
caption: "Del", buttonimg: "fullpath/row_del.gif", onClickButton: function(id) { alert("Deleting Row: " + id) }, position: "last"
});