GridView 后台分页
2016-05-18 10:36
218 查看
前面写了两篇 前台分页。但是数据少还行 ,条数上千,浏览器就反映迟钝,卡得要死,容易导致浏览器崩溃,用户体验非常不好 。
效果图
View Code
效果图
public partial class default: System.Web.UI.Page { SqlConnection connect; SqlCommand cmd; private int PageSize;//每页显示的条数 private int pageindex;//第几页 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //页面显示的条数 PageSize = int.Parse(ddlPageSize.SelectedItem.Value); //当前页 pageindex = 1; //绑定page的一些属性 Pagination pagination = PaginationFactory.Instance(PageSize, pageindex, RecordCount()); lblCurrentPage .Text = pageindex.ToString(); lblTotalPages.Text = pagination.TotalPages.ToString(); bind2(pageindex, PageSize); } } /// <summary> /// 选择显示数量 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { int pageSize = Convert.ToInt32(this.ddlPageSize.Text); Pagination pagination = PaginationFactory.Instance(pageSize, 1,RecordCount()); this.lblTotalPages.Text = pagination.TotalPages.ToString(); bind2( pagination.CurrentPage, pagination.PageSize); } /// <summary> /// 按页码 查询数据并绑定 /// </summary> /// <param name="currentPage">当前页</param> /// <param name="pageSize">页面显示条数</param> public void bind2(int currentPage, int pageSize) { string sql = ""; sql = @"select * from (select ROW_NUMBER() over (order by typeid) as HangHao, * from GoodsTypes) pageduserinfo where HangHao between (@currentPage -1)*@pagesize +1 and @pagesize * @currentPage"; using (connect = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=123;")) { connect.Open(); cmd = new SqlCommand(sql, connect); cmd.Parameters.AddWithValue("@currentPage", currentPage); cmd.Parameters.AddWithValue("@pagesize", pageSize); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataKeyNames = new string[] { "typeid" }; GridView1.DataBind(); lblCurrentPage.Text = currentPage.ToString(); } } /// <summary> /// 获取表的所有数据 /// </summary> /// <returns></returns> public int RecordCount() { int result = 0; string connStr = "server=.;database = Test;uid=sa;pwd=123"; string sql = "select count(*) from userinfo"; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); result = Convert.ToInt32(cmd.ExecuteScalar().ToString()); } return result; } /// <summary> /// 上一页 /// </summary> protected void btnPrev_Click(object sender, EventArgs e) { int pagSize = Convert.ToInt32(this.ddlPageSize.Text); int currentPage = Convert.ToInt32(this.lblCurrentPage.Text); Pagination p = PaginationFactory.Instance(pagSize, currentPage, RecordCount()); bind2(p.Prev, p.PageSize); } //下一页 protected void btnNext_Click(object sender, EventArgs e) { int pagSize = Convert.ToInt32(this.ddlPageSize.Text); int currentPage = Convert.ToInt32(this.lblCurrentPage.Text); Pagination p = PaginationFactory.Instance(pagSize, currentPage, RecordCount()); bind2(p.Next, p.PageSize); } /// <summary> /// 尾页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnLast_Click(object sender, EventArgs e) { int pagSize = Convert.ToInt32(this.ddlPageSize.Text); int currentPage = Convert.ToInt32(this.lblCurrentPage.Text);//可以不需要 Pagination p = PaginationFactory.Instance(pagSize, currentPage,RecordCount()); bind2( p.Last, p.PageSize); } /// <summary> /// 首页 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnFirst_Click(object sender, EventArgs e) { int pagSize = Convert.ToInt32(this.ddlPageSize.Text); int currentPage = 1; Pagination p = PaginationFactory.Instance(pagSize, currentPage, RecordCount()); bind2(p.CurrentPage, p.PageSize); } /// <summary> /// 跳转 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGo_Click(object sender, EventArgs e) { int pageSize = Convert.ToInt32(this.ddlPageSize.Text); int currentPage = Convert.ToInt32(this.txtToPage.Text); Pagination pagination = PaginationFactory.Instance(pageSize, currentPage, RecordCount()); this.txtToPage.Text = ""; bind2(pagination.CurrentPage, pagination.PageSize); } }
View Code
相关文章推荐
- 160518、java中使用百度地图(超级简单)
- systemd.service 中文手册
- Laravel Service Provider 中 boot 方法和 register 方法的区别
- 移动网页设计9大原则——第1部分
- tcpdump常用参数说明
- 关于UIScrollView上面布局遇到的问题
- 一点一点学ASP.NET之基础概念——HttpHandler
- static函数与普通函数
- windows下关于QT5连接mysql的几点问题
- spring整合Redis 入门例子
- 趋势科技搭建工控蜜罐系统
- iOS多线程同步锁
- Spark作业调度
- Spark中的Driver本质
- 在systemd(CentOS7)自启动zookeeper
- UILabel设置多种字体、颜色
- 1号店交易系统架构如何向「高并发高可用」演进
- 在systemd(CentOS7)自启动zookeeper
- 端口转为二级域名
- 《Secrets of the JavaScript Ninja》:JavaScript 之运行时代码