JQGrid+Nhibernate+Webservice+Linq

先上效果图:

image 

前台代码(jqgridtest.aspx):

<%@ 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 () {
            jQuery("#list2").jqGrid({
                url: 'JQGridService.asmx/JsonLinqData',
                data: {},
                datatype: 'json',
                mtype: 'POST',
                loadonce: true,
                ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
                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>

web服务代码(JQGridService.asmx):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using NHibernate;
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()
        {

            NHibernateHelper _helper = new NHibernateHelper();
            ISession _session = _helper.GetSession();
            ICriteria criteria = _session.CreateCriteria(typeof(Customer));
            criteria.SetMaxResults(25);
            IList<Customer> Customer = criteria.List<Customer>();

            var page = 2;
            var jsonData = new
            {
                page = page,
                total = 12,
                records = 25,
                rows = (
                        from bl in Customer
                        select new
                        {
                            id = bl.CustomerId,
                            cell = new string[] {
                                bl.CustomerId.ToString(),
                                bl.Firstname.ToString(),
                                bl.Lastname.ToString()}
                        }).ToArray()
            };

            return JsonConvert.SerializeObject(jsonData);

        }

    }
}
posted on 2013-07-10 11:06  vedusoft  阅读(330)  评论(0编辑  收藏  举报