ASP.NET WEBFORM + FlexiGrid + IBatis.NET 实现数据分页


ASP.NET WEBFORM + FlexiGrid

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Division.aspx.cs" Inherits="WebApplication1.Pages.Division.Division" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <link href="~/Content/fg/css/flexigrid.css" rel="stylesheet" />
    <script src="<%: ResolveUrl("~/Scripts/jquery-1.8.2.js") %>"></script>
    <script src="<%: ResolveUrl("~/Content/fg/js/flexigrid.js") %>"></script>
    <script src="<%: ResolveUrl("~/Content/layer/layer.js") %>"></script>

    <style>
        .hDivBox thead th,.bDiv tbody td{
            line-height:24px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table id="flex1" style="display:none;width:800px;"></table>
    <script>

        //在引入css的基础上配置skin参数,如下所示

        layer.config({
            skin: 'layer-ext-moon',
            extend: 'skin/moon/style.css'
        });



        function seldel(val, id) {

            val.innerHTML = '<input id=\"'+id+'\" type=\"checkbox\" >';

        }

        $(function () {
            $("#flex1").flexigrid({
                url: '/Ashxs/Region/Region.ashx',
                dataType: 'json',
                colModel: [
                    {display : '<input type=\"checkbox\" id=\"check_all\" >',
                        width : 30,
                        sortable : false,
                        align : 'center',
                        process:seldel
                    },
                    { display: 'ID', name: 'ID', width: 180, align: 'center' },
                    { display: 'Name', name: 'Name', width: 180, align: 'center' },
                    { display: 'Description', name: 'Description', width: 120, align: 'center' },
                    { display: 'CreatedBy', name: 'CreatedBy', width: 130, align: 'center' },
                    { display: 'CreatedDate', name: 'CreatedDate', width: 180, align: 'center' },
                    { display: 'ModifiedBy', name: 'ModifiedBy', width: 120, align: 'center' },
                    { display: 'ModifiedDate', name: 'ModifiedDate', width: 120, align: 'center' },
                    { display: 'Status', name: 'Status', width: 130, align: 'center' },
                    {
                        display: 'Actions',
                        width: 80,
                        sortable: false,
                        align: 'center',
                        process: function (val, id) { val.innerHTML = '<a href="#">sss</a>'; }
                    }
                ],
                sortname: "Name",
                resizable: false,
                sortorder: "asc",
                nowrap: false,
                minwidth: 160,
                usepager: true,
                striped: true,
                title: 'Region',
                rp: 10,
                idProperty: 'ID',
                width: 800,
                colResize: false,
                onSuccess: function () {
                    var w = 0;
                    w = $('.hDivBox').width() + 1;
                    $('.flexigrid').width(w);


                },
                height: 'auto',
                buttons: [
                    {
                        name: '添加', bclass: 'add', onpress: function () {
                            layer.open({
                                type: 2,
                                title: 'Edit',
                                shadeClose: true,
                                shade: 0.8,
                                area: ['580px', '350px'],
                                content: '/Pages/Division/Edit.aspx' //iframe的url
                            });
                        }
                    }, { separator: true },
                    { name: '删除', bclass: 'delete', onpress: function () { } }, { separator: true }
                ]
            });

            $('#check_all').live('click', function () {
                $('div.bDiv table tbody tr td:first-child :checkbox').prop('checked', $(this).prop('checked'));
            });

            $('div.bDiv table tbody tr td:first-child :checkbox').live('click', function () {
                var chks = $('div.bDiv table tbody tr td:first-child :checkbox');
                if (chks.length == chks.filter(":checked").length) {
                    console.log('Y');
                    $('#check_all').prop('checked', true);
                } else {
                    console.log('N');
                    $('#check_all').prop('checked', false);
                }
            });

        });
    </script>
    </form>
</body>
</html>

提供数据的 Generic Handler

namespace WebApplication1.Ashxs.Region
{
    /// <summary>
    /// Region 的摘要说明
    /// </summary>
    public class Region : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.Params["action"] ?? "list";

            switch (action.ToLower())
            {
                case "list":
                    PagedList(context);
                    break;
                case "edit":
                    UpdateOne(context);
                    break;
                default:
                    break;
            }

        }

        public void UpdateOne(HttpContext context)
        {
            RegionModel p = new RegionModel();
            p.ID = 1;
            p.Name = "North";
            RegionDao dao = new RegionDao();
            dao.UpdateOne(p);
        }

        public void List(HttpContext context)
        {
            RegionDao dao = new RegionDao();
            IList<RegionModel> list = dao.GetList();

            string json = JsonConvert.SerializeObject(new { total = 4, page = 1, rows = list });

            context.Response.ContentType = "text/plain";
            context.Response.Write(json);
        }

        public void PagedList(HttpContext context)
        {
            int page = Convert.ToInt32(context.Request.Params["page"]) < 1 ? 1 : Convert.ToInt32(context.Request.Params["page"]);
            int rp = Convert.ToInt32(context.Request.Params["rp"]);

            int start = (page - 1) * rp;
            int end = start + rp;

            RegionDao dao = new RegionDao();
            IList<RegionModel> list = dao.GetPagedList(new PagerModel { Start = start, End = rp });

            int recordCount = dao.Count();
            int pageCount = recordCount / rp + 1;

            string json = JsonConvert.SerializeObject(new { total = recordCount, page = page, rows = list });

            context.Response.ContentType = "text/plain";
            context.Response.Write(json);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

IBatis.NET sqlMap文件

<?xml version="1.0" encoding="utf-8" ?>
<sqlMap namespace="Ibatis" xmlns="http://ibatis.apache.org/mapping" xmlns:xls="http://www.w3.org/2001/XMLSchema-instance">
  <statements>
    <select id="SelectAllRegion" resultClass="WebApplication1.Model.RegionModel">
      select * from region
    </select>

    <select id="CountAllRegion" resultClass="System.Int32">
      select count(*) from region
    </select>

    <select id="SelectPagedRegion" resultClass="WebApplication1.Model.RegionModel">
      select * from( select *, row_number() over ( order by id) as rank  from region) as t where t.rank between #Start# and #End#
    </select>
    <update id="UpdateOne" resultMap="RegionModel">
      update region set name = #Name# Where Id = #ID#
    </update>
  </statements>

</sqlMap>

结果图

知识共享许可协议
《ASP.NET WEBFORM + FlexiGrid + IBatis.NET 实现数据分页(一)》 常伟华 创作。
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议 | 3.0 中国大陆许可协议进行许可。

站内公告

A PHP Error was encountered

Severity: Core Warning

Message: PHP Startup: zip: Unable to initialize module Module compiled with module API=20060613 PHP compiled with module API=20090626 These options need to match

Filename: Unknown

Line Number: 0

Backtrace: