分页

本文介绍了一种基于ASP.NET的简单分页实现方法,通过使用静态变量存储页码和页大小,并通过调用存储过程加载指定页的数据到GridView中。同时,文章还展示了如何通过页面导航来更新显示的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 public partial class Index : System.Web.UI.Page
    {
        public static int pageIndex = 1;//初始页码
        public int pageSize = 10;//初始页数量
        public static int  pageCount = 0;//总页数
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取直接跳转的页号
            if (Request["page"] != null)
            {
                //判断当前跳转的页数是否大于总页数,如果大于则直接跳转至最后一页
                pageIndex = int.Parse(Request["page"].ToString());
                if (pageIndex > pageCount)
                    pageIndex = pageCount;
                else if (pageIndex < 1)
                    pageIndex = 1;
                LoadGrid(pageSize, pageIndex,ref pageCount);
            }

            if (!IsPostBack)
            {
                LoadGrid(pageSize, pageIndex, ref pageCount);
            }

        }

        /// <summary>
        /// 执行分页存储过程
        /// </summary>
        /// <param name="pageSize">页显示数量</param>
        /// <param name="pageIndex">页码</param>
        private void LoadGrid(int pageSize,int pageIndex,ref int pageCount)
        {
            //string sqlStr = "select * from shop where shopid = @id";
            //SqlParameter[] pars = new SqlParameter[]{
            //    new SqlParameter("@id",56)
            //};

            string sqlStr = "proc_pager";
            SqlParameter[] pars = new SqlParameter[] { 
                new SqlParameter("@pageSize",pageSize),//页数量
                new SqlParameter("@pageIndex",pageIndex),//页码
                new SqlParameter("@pageCount",pageCount)//总页数(输出参数)
            };
            pars[2].Direction = ParameterDirection.Output;//设置参数为输出参数
            this.GridView1.DataSource = DBHelper.ExecuteTable(sqlStr, CommandType.StoredProcedure, pars);
            this.GridView1.DataBind();
            pageCount = int.Parse(pars[2].Value.ToString());//将存储过程输出值赋值给pageCount
            this.lab_PageCount.Text = pageCount.ToString();

            //禁用和启用上一页
            if (pageIndex == 1)
                this.first_Hover.Enabled = false;
            else
                this.first_Hover.Enabled = true;

            //禁用和启用下一页
            if (pageIndex >= pageCount)
                this.Last_Hover.Enabled = false;
            else
                this.Last_Hover.Enabled = true;

        }

        protected void first_Hover_Click(object sender, EventArgs e)
        {
            pageIndex--;//当点击上一页时,将页码-1
            Response.Redirect("index.aspx?page=" + pageIndex);
            //LoadGrid(pageSize, pageIndex, ref pageCount);
        }

        protected void Last_Hover_Click(object sender, EventArgs e)
        {
            pageIndex++;//当点击下一页时,将页码+1
            Response.Redirect("index.aspx?page=" + pageIndex);
            //LoadGrid(pageSize, pageIndex, ref pageCount);
        }
    }


数据源分页

   public string PageJSON(int row, int page, IList<model> list)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            int pagecount = list.Count();
            var query = list.ToList().Skip(row * (page - 1)).Take(row);
            return "{\"total\":" + pagecount + ",\"rows\":" + jss.Serialize(query) + "}";
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值