当前位置: 首页 > news >正文

Linq实现GridView高效分页(Skip() and take())


1.页面代码

Code
<!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>
</head>
<body>
    
<form id="form1" runat="server">
    
<asp:GridView  ID="grdView"  runat="server">
    
</asp:GridView>
            
<asp:Label runat="server" ID="lblRowsCount"></asp:Label>
            
<asp:Button ID="btnFirst" Text="首页" runat="server" OnClick="btnFirst_Click">
            
</asp:Button>
            
<asp:Button ID="btnPre" Text="上一页" runat="server" OnClick="btnPre_Click">
            
</asp:Button>
            
<asp:Button ID="btnNext" Text="下一页" runat="server" OnClick="btnNext_Click">
            
</asp:Button>
            
<asp:Button ID="btnLast" Text="末页" runat="server" OnClick="btnLast_Click">
            
</asp:Button>
            
<asp:Label runat="server" ID="lblPagesIndex"></asp:Label>
            
<asp:Label runat="server" ID="Label2" Text="转到第:"></asp:Label>
            
<asp:TextBox ID="txt" runat="server"  Width="40px" ></asp:TextBox>
            
<asp:Label runat="server" ID="Label1" Text=""></asp:Label>
            
<asp:Button ID="btnSkip" Text="转到" runat="server" OnClick="btnSkip_Click"></asp:Button>
          
    
</form>
</body>
</html>

2.后台代码

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaiChang.Node;


public partial class admin_Club_User_test : System.Web.UI.Page
{
    NodeContext nc 
= new NodeContext(Utils.BC_ConnectionString);
    
    
protected void Page_Load(object sender, EventArgs e)
    {
        
this.pageSize = 20;
        
if (!IsPostBack)
        {
            SetFirstPage();
        }
    }

    
protected override void OnPreRender(EventArgs e)
    {
        
base.OnPreRender(e);
        
if (CurrentPage == 1)
        {
            btnFirst.Enabled 
= false;
            btnPre.Enabled 
= false;
        }
        
else
        {
            btnFirst.Enabled 
= true;
            btnPre.Enabled 
= true;
        }
        
if (CurrentPage == PageCount)
        {
            btnNext.Enabled 
= false;
            btnLast.Enabled 
= false;
        }

        
else
        {
            btnNext.Enabled 
= true;
            btnLast.Enabled 
= true;
        }
        GetCurrent(CurrentPage, PageSize);
        lblRowsCount.Text 
= string.Format("共:{0} 位会员", RowsCount);
        lblPagesIndex.Text 
= string.Format("页次:{0}/{1}", CurrentPage, PageCount);
    }

    
protected void btnFirst_Click(object sender, EventArgs e)
    {
        SetFirstPage();
    }
    
    
protected void btnLast_Click(object sender, EventArgs e)
    {
        SetLastPage();
    }

    
protected void btnPre_Click(object sender,EventArgs e)
    {
        
if (ViewState["currentPage"== null)
        {
            
this.currentPage = 1;
        }
        
else
        {
            
this.currentPage = int.Parse(ViewState["currentPage"].ToString()) - 1;
            ViewState[
"currentPage"= currentPage;
        }
    }
    
    
protected void btnNext_Click(object sender, EventArgs e)
    {
        
if (ViewState["currentPage"!= null)
        {
            
this.currentPage = int.Parse(ViewState["currentPage"].ToString()) + 1;
            ViewState[
"currentPage"= currentPage;
        }
    }

    
protected void btnSkip_Click(object sender, EventArgs e)
    {
        
if (txt.Text.Trim() == string.Empty)
        {
            
return;
        }
        
else
        {
            
int skipPage = int.Parse(txt.Text.Trim());
            
if (skipPage > PageCount)
            {
                txt.Text 
= string.Empty;
                
return;
            }
            
else
            {
                
this.currentPage = skipPage;
                ViewState[
"currentPage"= currentPage;
            }
        }
    }

    
void SetFirstPage()
    {
        
this.currentPage = 1;
        ViewState[
"currentPage"= currentPage;
    }

    
void SetLastPage()
    {
        
this.currentPage = PageCount;
        ViewState[
"currentPage"= currentPage;
    }

    IEnumerable
<BC_Club> Select()
    {
        var clubs 
= nc.BC_Club.Where(m => m.FilterState != 99);
        
return clubs;
    }

    
void GetCurrent(int start,int rows)
    {
        grdView.Rows.Clear();
        grdView.DataSource 
= Select().Skip(pageSize * (start - 1)).Take(rows).ToList();
        grdView.DataBind();
    }

    
private int RowsCount
    {
        
get 
        { 
            
if( ViewState["RowsCount"]==null)
                
return Select().Count(); 
            
else
                
return int.Parse(ViewState["RowsCount"].ToString());

        }
    }
   
    
private int pageSize;
    
private int PageSize
    {
        
set { value = this.pageSize; }
        
get { return pageSize; }
    }
    
    
private int currentPage;
    
private int CurrentPage
    {
        
set { value = this.currentPage; }
        
get { return this.currentPage; }
    }

    
private int PageCount
    {
        
get { return (int)Math.Ceiling((double)RowsCount / PageSize); }
    }


    
}


注释:

转载于:https://www.cnblogs.com/extensivewang/archive/2009/09/28/1575934.html

相关文章:

  • 我从11楼跳下去。。。
  • ASP.NET获取客户端IP/用户名等信息
  • mysql noinstall 安装
  • asp.NET 脏字过滤算法 修改版
  • 起个名儿,叫如何自主学习(网络版)~~
  • 整理网上的一些关于sharepoint编码方面的最佳实践
  • 基于MapWinGis的开发探索(四)——图层窗口、地名检索的实现
  • 中间件
  • ArcEngine开发点滴1
  • [转]ATOM 规范 中文版
  • javascript实现组合的递归算法及变种
  • 汇报方案
  • [转]深一层看依赖注入
  • C# 中的委托和事件 copyright http://www.cnblogs.com/JimmyZhang
  • Week Function
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • AHK 中 = 和 == 等比较运算符的用法
  • Android开源项目规范总结
  • JavaScript异步流程控制的前世今生
  • Koa2 之文件上传下载
  • Solarized Scheme
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • windows下如何用phpstorm同步测试服务器
  • 浮现式设计
  • 前端技术周刊 2019-02-11 Serverless
  • 前端面试题总结
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • 一个SAP顾问在美国的这些年
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 在Unity中实现一个简单的消息管理器
  • 最近的计划
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • #NOIP 2014#Day.2 T3 解方程
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • #微信小程序:微信小程序常见的配置传旨
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (SpringBoot)第二章:Spring创建和使用
  • (附源码)spring boot车辆管理系统 毕业设计 031034
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (七)Knockout 创建自定义绑定
  • (转)IOS中获取各种文件的目录路径的方法
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .net framework4与其client profile版本的区别
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .NET(C#) Internals: as a developer, .net framework in my eyes
  • .NET/C# 使窗口永不获得焦点
  • .net和jar包windows服务部署