<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jqgridtest.aspx.cs" Inherits="WebNhibernate4.jqgridtest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link id="uiThemes" rel="stylesheet" type="text/css" media="screen" href="Pub/jqgrid/css/ui-lightness/jquery-ui-1.8.13.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="Pub/jqgrid/css/ui.jqgrid.css" />
<script type="text/javascript" src="Pub/jqgrid/js/jquery-1.5.2.min.js"></script>
<script src="Pub/jqgrid/js/i18n/grid.locale-cn.js" type="text/javascript"></script>
<script src="Pub/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
postData = { wangbf: 'test', company: 'fubo' };
jQuery("#list2").jqGrid({
url: 'JQGridService.asmx/JsonLinqData',
datatype: 'json',
mtype: 'POST',
//loadonce: true,
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
postData:{wangbf:'1test',company:'fubo'},
serializeGridData: function (postData) {
return JSON.stringify(postData);
},
jsonReader: {
root: function (obj) {
var data = eval("(" + obj.d + ")");
return data.rows;
},
page: function (obj) {
var data = eval("(" + obj.d + ")");
return data.page;
},
total: function (obj) {
var data = eval("(" + obj.d + ")");
return data.total;
},
records: function (obj) {
var data = eval("(" + obj.d + ")");
return data.records;
}
},
colNames: ['Inv No', 'firstname', 'lastname'],
colModel: [
{ name: 'CustomerId', index: 'CustomerId', width: 55 },
{ name: 'Firstname', index: 'Firstname', width: 90 },
{ name: 'Lastname', index: 'Lastname', width: 100 }
],
rowNum: 10,
width: 600,
rowList: [10, 20, 30],
pager: '#pager2',
sortname: 'Firstname',
viewrecords: true,
sortorder: "asc",
caption: "JSON Example"
});
jQuery("#list2").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false });
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="list2" style="width:600px;"></table> <div id="pager2"></div>
</div>
</form>
</body>
</html>
注意:postData是用户自定的属性;
Webservice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using NHibernate;
using NHibernate.Criterion;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace WebNhibernate4
{
/// <summary>
/// JQGridService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class JQGridService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public string JsonLinqData(bool? _search, string nd, int page, int rows, string sidx, string sord)
{
NHibernateHelper _helper = new NHibernateHelper();
ISession _session = _helper.GetSession();
ICriteria criteria = _session.CreateCriteria(typeof(Customer));
//criteria.SetMaxResults(25);
if(sord=="asc")
{
criteria.AddOrder(Order.Asc(sidx));
}else
criteria.AddOrder(Order.Desc(sidx));
IList<Customer> lstCustomer = criteria.List<Customer>();
var jsonData = new
{
page = page,
total = Math.Ceiling(lstCustomer.Count / 10.0),
records =lstCustomer.Count ,
rows = (
from bl in lstCustomer
select new
{
id = bl.Customerid,
cell = new string[] {
bl.Customerid.ToString(),
bl.Firstname.ToString(),
bl.Lastname.ToString()}
}).ToArray()
};
return JsonConvert.SerializeObject(jsonData);
}
}
}