分页效果(无刷新)

---------------------------------存储过程--------------------------

ALTER proc [dbo].[pro_fenye]
@pagesize int,
@pageindex int
as
select * from(select ROW_NUMBER() over(order by Id) as rownumber,Id,NewsTitle,Substring(NewsContent,0,20) as NewsContent,CreateTime from T_News1)T
WHERE rownumber>(@pageindex-1)*@pagesize AND rownumber<=(@pageindex*@pagesize

-------------------------------DAL层方法-----------------------------

///分页获取

public DataTable GetListDataTable(int PageSize, int PageIndex)
        {
            SqlParameter[] parameters = {
     
     new SqlParameter("@PageSize", SqlDbType.Int),
     new SqlParameter("@PageIndex", SqlDbType.Int)
     };
           
            parameters[0].Value = PageSize;
            parameters[1].Value = PageIndex;
            return DbHelperSQL.RunProcedureDataTable("pro_fenye", parameters);
        }

 

/// <summary>
  /// 获取记录总数
  /// </summary>
  public int GetRecordCount(string strWhere)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select count(1) FROM T_News1 ");
   if(strWhere.Trim()!="")
   {
    strSql.Append(" where "+strWhere);
   }
   object obj = DbHelperSQL.GetSingle(strSql.ToString());
   if (obj == null)
   {
    return 0;
   }
   else
   {
    return Convert.ToInt32(obj);
   }
  }

------------------------------------BLL层方法-----------------------------------

///分页方法

public DataTable GetListDataTable(int pagesize, int pageindex)
        {
            return dal.GetListDataTable(pagesize, pageindex);
        }

/// <summary>
  /// 分页获取数据列表总条数
  /// </summary>
  public int GetRecordCount(string strWhere)
  {
   return dal.GetRecordCount(strWhere);
  }

--------------------------------------html页-----------------------------

 <script src="js/Jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var pageindex = 1;
            var pagesize = 10;
            var lastpageindex = 1;
            /*如果将代码封装成一个函数,那么除非显示调用(loaddata()),否则函数中的代码不会执行
            根据传递的页码和每页显示的记录数量获取数据
            */
            function loaddata() {
                $.ajax({
                    type: "post",
                    contentType: "application/json",
                    url: "WebService1.asmx/GetListAjax",
                    data: "{pagesize:" + pagesize + ",pageindex:" + pageindex + "}",
                    success: function (result) {
                        var strtable = '<table>';
                        strtable += '<tr><td>编号</td><td>标题</td><td>内容</td><td>创建时间</td></tr>';
                        for (var i = 0; i < result.d.length; i++) {
                            strtable += '<tr>';
                            strtable += '<td>' + result.d[i].Id + '</td>';

                            strtable += '<td>' + result.d[i].NewsTitle + '</td>';

                            strtable += '<td>' + result.d[i].NewsContent + '</td>';

                            strtable += '<td>' + result.d[i].CreateTime + '</td>';
                            strtable += '</tr>';
                        }
                        strtable += '</table>';
                        $('#mydiv').html(strtable);
                    }
                })
            }

            //根据传递到后台的每页显示的记录数量来获取最大的页码(就是一共有多少页)
            $.ajax({
                type: "post",
                contentType: "application/json",
                url: "WebService1.asmx/GetLastPageindex",
                data: "{pagesize:" + pagesize + "}",
                success: function (result) {
                    lastpageindex = result.d;
                }
            })

            //显式调用函数,在页面初次加载时加载第一页数据
            loaddata();
            //下一页
            $('a:eq(2)').click(function () {
                if (pageindex < lastpageindex) {
                    pageindex++;
                    loaddata();
                }

            })
            //上一页
            $('a:eq(1)').click(function () {
                if (pageindex > 1) {
                    pageindex--;
                    loaddata();
                }
            })
            //第一页
            $('a:first').click(function () {
                pageindex = 1;
                loaddata();
            })
            //最后一页
            $('a:eq(3)').click(function () {
                pageindex = lastpageindex;
                loaddata();
            })
            $('a:last').click(function () {
                pageindex = $('#txtPageindex').val();
                loaddata();
            })
        })
    </script>
</head>
<body>
<div id="mydiv">
</div>
<div><a href="#">第一页</a><a href="#">上一页</a><a href="#">下一页</a><a href="#">最后一页</a><input
        id="txtPageindex" type="text" /><a href="#">Go</a></div>
</body>
</html>

-------------------------------WebService1.asmx---------------------------------

 [WebMethod]
        public List<Model.T_News1> GetListAjax(int pagesize, int pageindex)
        {
            BLL.T_News1 bnews = new BLL.T_News1();
            DataTable dt= bnews.GetListDataTable(pagesize, pageindex);

            List<Model.T_News1> list = new List<Model.T_News1>();

            int Id;
            string newstitle = "";
            string newscontent = "";
            DateTime createtime;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Id = Convert.ToInt32(dt.Rows[i]["Id"]);
                newstitle = dt.Rows[i]["NewsTitle"].ToString();
                newscontent = dt.Rows[i]["NewsContent"].ToString();
                createtime = Convert.ToDateTime(dt.Rows[i]["CreateTime"]);

                Model.T_News1 news = new Model.T_News1()
                {
                    Id = Id,
                    NewsTitle = newstitle,
                    NewsContent = newscontent,
                    CreateTime = createtime
                };
                list.Add(news);
            }
            return list;
        }
        [WebMethod]
        public int GetLastPageindex(int pagesize)
        {
            BLL.T_News1 bnews = new BLL.T_News1();
            int totalcount= bnews.GetRecordCount("");
            if (totalcount % pagesize == 0)
            {
                return totalcount / pagesize;
            }
            else
            {
                return totalcount / pagesize + 1;
            }
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值