用临时表的GridView分页

 本例子采用sql2000下的Nowthwind数据库中的[Order Details]表

下面是存储过程脚本

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1ALTER    PROC OrderDetailsPaging
 2(@PageIndex int,--页码
 3 @PageSize int,--页尺寸
 4 @RowsCount int output)--总行数
 5AS
 6BEGIN
 7set nocount on
 8declare @PageLowerBound int
 9declare @PageUpperBound int
10declare @RowsToReturn int
11
12set @PageLowerBound=@PageIndex*@PageSize+1
13set @PageUpperBound=(@PageIndex+1)*@PageSize
14set @RowsToReturn=@PageUpperBound
15
16set rowcount @RowsToReturn
17--创建带一个自增键的临时表
18create table #PageIndex
19(IndexID int identity(1,1) not null,
20 OrderDetailsID int,
21 ProductID int)
22insert into #PageIndex(OrderDetailsID,ProductID)
23select OrderID,ProductID from [Order Details]
24order by OrderID asc
25
26select @RowsCount=count(OrderID) from [Order Details]
27
28select pageindex.IndexID,od.OrderID,od.ProductID,od.UnitPrice,od.Quantity,od.Discount 
29from [Order Details] od inner join #PageIndex pageindex on od.OrderID=pageindex.OrderDetailsID
30where pageindex.IndexID >= @PageLowerBound and pageindex.IndexID<= @PageUpperBound and 
31pageindex.ProductID=od.ProductID
32
33END
34
35set nocount off
36set rowcount 0
37
38GO

 

以下是页面的隐藏代码

 

ContractedBlock.gifExpandedBlockStart.gifCode
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.Data.SqlClient;
 11
 12public partial class _Default : System.Web.UI.Page 
 13ExpandedBlockStart.gifContractedBlock.gif{
 14    private const string sql_select_orderDetails = "select OrderID,ProductID,UnitPrice,Quantity,Discount from [Order Details]";
 15    private const string sql_select_Categories = "select CategoryID,CategoryName,Description,Picture from Categories";
 16    // 总记录数
 17    private static int Rows;
 18    // 当前页数
 19    private static int CurrentPageIndex=0;
 20    // 总页数
 21    private static int PageCount = -1;
 22    protected void Page_Load(object sender, EventArgs e)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 24        if (!Page.IsPostBack)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 26            this.BindData();
 27        }

 28    }

 29    private void BindData()
 30ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 31        this.GridView1.DataSource = GetOrderDetails(CurrentPageIndex,GridView1.PageSize);
 32        this.GridView1.DataBind();
 33    }

 34    private DataSet GetReportCategories()
 35ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 36        return DBUtility.SQLAccess.ExecuteDataSet(DBUtility.SQLAccess.ConnectionString, CommandType.StoredProcedure, sql_select_orderDetails, null);
 37    }

 38    private DataSet GetCategoryies()
 39ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 40        return DBUtility.SQLAccess.ExecuteDataSet(DBUtility.SQLAccess.ConnectionString, CommandType.Text, sql_select_Categories, null);
 41    }

 42ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 43    /// 返回OrderDetails表中的数据
 44    /// </summary>
 45    /// <returns></returns>j

 46    private DataSet GetOrderDetails(int pageindex,int pagesize)
 47ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 48        SqlParameter[] orderDetails_pars = new SqlParameter[3];
 49        orderDetails_pars[0= new SqlParameter("@PageIndex", SqlDbType.Int);
 50        orderDetails_pars[0].Value = pageindex;
 51        orderDetails_pars[1= new SqlParameter("@PageSize", SqlDbType.Int);
 52        orderDetails_pars[1].Value = pagesize;
 53        orderDetails_pars[2= new SqlParameter("@RowsCount", SqlDbType.Int);
 54        orderDetails_pars[2].Direction = ParameterDirection.Output;
 55        DataSet ds = DBUtility.SQLAccess.ExecuteDataSet(DBUtility.SQLAccess.ConnectionString, CommandType.StoredProcedure, "OrderDetailsPaging", orderDetails_pars);
 56        Rows = (int)orderDetails_pars[2].Value;
 57        PageCount = this.GetPageCount(this.GridView1.PageSize, Rows);
 58        return ds;
 59    }

 60    private DataSet GetAllOrderDetails()
 61ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 62        return DBUtility.SQLAccess.ExecuteDataSet(DBUtility.SQLAccess.ConnectionString, CommandType.Text, sql_select_orderDetails, null);
 63    }

 64    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 66        this.GridView1.PageIndex = e.NewPageIndex;
 67        this.GridView1.DataSource = this.GetOrderDetails(e.NewPageIndex, GridView1.PageSize);
 68        Response.Write(((DataSet)GridView1.DataSource).Tables[0].Rows.Count);
 69        this.GridView1.DataBind();
 70    }

 71    protected void lbtn_First_Click(object sender, EventArgs e)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 73        CurrentPageIndex = 0;
 74        this.GridView1.DataSource = GetOrderDetails(CurrentPageIndex, GridView1.PageSize);
 75        this.GridView1.DataBind();
 76    }

 77    protected void lbtn_Pre_Click(object sender, EventArgs e)
 78ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 79        if ((--CurrentPageIndex) < 0)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 81            CurrentPageIndex++;
 82            return;
 83        }

 84        else
 85ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 86            this.GridView1.DataSource = GetOrderDetails(CurrentPageIndex, GridView1.PageSize);
 87            this.GridView1.DataBind();
 88        }

 89    }

 90ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 91    /// 下一页按钮
 92    /// </summary>
 93    /// <param name="sender"></param>
 94    /// <param name="e"></param>

 95    protected void lbtn_Next_Click(object sender, EventArgs e)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 97        // 先判断当前页索引
 98        if ((++CurrentPageIndex) > PageCount-1)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        {
100            CurrentPageIndex--;
101            return;
102        }

103        else
104ExpandedSubBlockStart.gifContractedSubBlock.gif        {
105            this.GridView1.DataSource = GetOrderDetails(CurrentPageIndex, GridView1.PageSize);
106            this.GridView1.DataBind();
107        }

108    }

109    protected void lbtn_Last_Click(object sender, EventArgs e)
110ExpandedSubBlockStart.gifContractedSubBlock.gif    {
111        CurrentPageIndex = PageCount - 1;
112        this.GridView1.DataSource = GetOrderDetails(CurrentPageIndex, GridView1.PageSize);
113        this.GridView1.DataBind();
114    }

115ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
116    /// 计算页数
117    /// </summary>
118    /// <param name="pagesize"></param>
119    /// <param name="rows"></param>
120    /// <returns></returns>

121    private int GetPageCount(int pagesize, int rows)
122ExpandedSubBlockStart.gifContractedSubBlock.gif    {
123        return (rows + pagesize - 1/ pagesize;
124    }

125}

126

以下是页面设计代码

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1ExpandedBlockStart.gifContractedBlock.gif<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 2
 3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4
 5<html xmlns="http://www.w3.org/1999/xhtml" >
 6<head runat="server">
 7    <title>无标题页</title>
 8</head>
 9<body>
10    <form id="form1" runat="server">
11    <div>
12        &nbsp;
13        <div style="font-size: 12px; z-index: 101; left: 92px; width: 542px; position: absolute;
14            top: 76px; height: 276px">
15        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" BorderStyle="None"
16            OnPageIndexChanging="GridView1_PageIndexChanging" GridLines="Horizontal" HorizontalAlign="Center" Width="100%">
17            <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" Visible="False" />
18            <RowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
19            <HeaderStyle BorderStyle="Dotted" />
20            <AlternatingRowStyle BorderStyle="Dotted" HorizontalAlign="Center" VerticalAlign="Middle" />
21        </asp:GridView>
22            &nbsp; &nbsp;&nbsp;
23            <div align="center" nowrap="nowrap" style="font-size: 12px; z-index: 101; left: 183px;
24                width: 152px; position: absolute; top: 246px; height: 15px">
25                <asp:LinkButton ID="lbtn_First" runat="server" OnClick="lbtn_First_Click">首页</asp:LinkButton>
26                <asp:LinkButton ID="lbtn_Pre" runat="server" OnClick="lbtn_Pre_Click">上一页</asp:LinkButton>
27                <asp:LinkButton ID="lbtn_Next" runat="server" OnClick="lbtn_Next_Click">下一页</asp:LinkButton>
28                <asp:LinkButton ID="lbtn_Last" runat="server" OnClick="lbtn_Last_Click">尾页</asp:LinkButton></div>
29        </div>
30    
31    </div>
32    </form>
33</body>
34</html>

 

此存储过程利用临时表来分页,并不通用,也不带排序,我想排序可以由服务器来完成,

而没有必要由数据库来做。

还有待进一步完善。

如需让以上代码正常运行,还需要写一个ExecuteDataSet方法来完成从数据库中读取数据的功能。

转载于:https://www.cnblogs.com/cykevin/articles/1293605.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463756.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HTML与CSS(图解6):超链接

动态的超链接&#xff1a; <html> <head> <title>动态超链接</title> <style> <!-- body{background:url(bg9.gif); /* 页面背景图片 */margin:0px; padding:0px;cursor:pointer; /*意思就是鼠标指针变成 手 的形状&#xff0c;和放到链…

pointcut 切面表达式 切入点表达式

下面给出一些常见切入点表达式的例子。 任意公共方法的执行&#xff1a; execution(public * *(..)) 任何一个以“set”开始的方法的执行&#xff1a; execution(* set*(..)) AccountService 接口的任意方法的执行&#xff1a; execution(* com.xyz.service.AccountService.*…

keil分散加载文件浅析

什么是分散加载文件分散加载文件&#xff08;scatter file&#xff09;是一个文本文件&#xff0c;它的作用是可以用于描述 ARM 链接器生成映像文件所需要的信息。如果不使用 scatter file 文件来指定&#xff0c;那么 ARM 链接器会按照默认的方式来生成映像文件&#xff0c;但…

socket buffer套接字缓存

最近公司在开发机器人与服务器调度端的通信时需要使用socket&#xff0c;因此找到了该文章作为深刻理解socket内部运作。 Linux网络核心数据结构是套接字缓存(socket buffer)&#xff0c;简称skb。它代表一个要发送或处理的报文&#xff0c;并贯穿于整个协议栈。 1、 套接字…

LAMP 系统性能调优,第 3 部分: MySQL 服务器调优(转)

关于 MySQL 调优 有 3 种方法可以加快 MySQL 服务器的运行速度&#xff0c;效率从低到高依次为&#xff1a; 替换有问题的硬件。对 MySQL 进程的设置进行调优。对查询进行优化。迁移到 DB2? 您正在寻找一种干净利落、无成本的方法用来从 MySQL 迁移到 IBM? DB2? 吗&#xf…

Python--day 3

1 # -*- coding:utf-8 -*-2 # Author:Monarch-T3 4 for 循环5 for i in range(10):6 print("Loop:", i)7 8 for i in range(0, 10, 2): #步长29 continue 跳出本次循环进入下次循环 10 break 结束循环 转载于:https://www.cnblogs.com/Monarch-T/p/10245724…

C语言中的常用文件操作

原文链接 常常觉得&#xff0c;我对很多东西都是要求会用就好&#xff0c;不求甚解。比如说每次一遇到文件操作&#xff0c;我必要查查相关的API和例子&#xff0c;然后依样画葫芦写下来。或许正是因为这种不求甚解的态度&#xff0c;让我一直处于半桶水的状态。看完了《C专家编…

圆梦,手写了个操作系统

大家好&#xff0c;我是发哥。我不止一次在公众号上强调学习操作系统的重要性。至于学习的方法&#xff0c;无外乎看书、看视频、看源码等等。也有推荐过跟着老师一起手写操作系统&#xff0c;但很少有学习方法能同时兼顾以下三点&#xff1a;1、Linux内核2、Windows内核3、自己…

Socket api接口--Send(),Recv()的长度问题

一个包没有固定长度&#xff0c;以太网限制在46&#xff0d;1500字节&#xff0c;1500就是以太网的MTU&#xff0c;超过这个量&#xff0c;TCP会为IP数据报设置偏移量进行分片传输&#xff0c;现在一般可允许应用层设置8k&#xff08;NTFS系统&#xff09;的缓冲区&#xff0c;…

安卓打包apk

打apk包的环境依赖 1.jdk 2.sdk 3.ndk 打apk包的工具 gradle mkdir /usr/local/Android cd /usr/local/Android mkdir sdk gradle ndk 1.jdk安装 略 2.sdk安装 https://www.androiddevtools.cn/下载地址 cd /usr/local/Android/sdk wget https://dl.google.com/android/andro…

收集最全的Joomla教材网站和joomla模板网站

http://www.seonote.net/joomla/the-most-complete-collection-of-joomla-sites-and-joomla-templates-website-materials.html 这篇文章算是为joomla学习者而准备的&#xff0c;包括了适合初学者学习的教材网站、国内比较好的joomla学习网 站以及几个比较好的joomla模板网站&a…

你知道怎么衡量硬件设备的算力吗?

前几天在知乎上看到有知友提问&#xff0c;什么是 GPU 算力。当时简单回答了一下&#xff0c;今天有空&#xff0c;在这里详细谈谈算力。算力也是做高性能计算的核心概念和指标。设备算力分为两部分&#xff0c;其一是设备&#xff0c;其二是算力。设备主要是指 CPU、GPU、DSP、…

socket api中send()和recv()函数工作原理与要点

send()和recv()函数是网络编程中经常使用到的函数&#xff0c;下面详细的比较两者的不同之处 send函数工作原理&#xff1a;send函数只负责将数据提交给协议层。 当调用该函数时&#xff0c;send先比较待发送数据的长度len和套接字s的发送缓冲区的长度&#xff0c;如果len大于s…

Python爬虫入门教程 22-100 CSDN学院课程数据抓取

1. CSDN学院课程数据-写在前面 今天又要抓取一个网站了&#xff0c;选择恐惧症使得我不知道该拿谁下手&#xff0c;找来找去&#xff0c;算了&#xff0c;还是抓取CSDN学院吧&#xff0c;CSDN学院的网站为 https://edu.csdn.net/courses 我看了一下这个网址&#xff0c;课程数量…

UML类图解义 (来自《大话设计模式》)

为什么80%的码农都做不了架构师&#xff1f;>>> 小菜&#xff1a;“对了&#xff0c;我时常在一些技术书中看到这些类图表示&#xff0c;简单的还看得懂&#xff0c;有些标记我很容易混淆。要不你给我讲讲吧。” 大鸟&#xff1a;“这个其实多看多用就熟悉了。我给…

易写易库(EXEK)玩“花”儿之三:命令有图标支持库,附图

释题&#xff1a;如果您认为这个“花”儿&#xff0c;属于之前第一个“花”儿的变种&#xff0c;易语言写支持库也能玩出“花”儿来&#xff08;易写易库(EXEK)进展5&#xff09;&#xff0c;我也不反对。 在开始正文之前&#xff0c;我要首先回答我之前提出的两个问题&#xf…

您好,有什么嵌入式书籍推荐的?

回答下这个后台的留言-----比较推荐下面这个仓库里面的内容&#xff01;https://github.com/ZhongYi-LinuxDriverDev/CS-EmbeddedLinux-Book#%E5%85%A8%E9%83%A8%E6%B1%87%E6%80%BB

闭包的功能举例

闭包可以在函数外部或者其他函数内,访问本函数内的变量: 闭包可以使变量持久,常驻内存,又可以避免变量被外部修改 1 def func():2 name "大傻子" #定义个name "大傻子"3 def func1():4 return name #把name返回给…

低学历者为何能骗取30万年薪职位

内容&#xff1a;<P>  在如今的职场就业中&#xff0c;高学历真的很诱人。号称是美国耶鲁大学管理学博士但只有初中毕业文凭的方某&#xff0c;能不受到高企的忠爱?在方某的精心准备&#xff0c;经历过一系列面试&#xff0c;他终于谋得了一份年薪30万的总经理助理兼人…

VS2017打开低版本的VS MVC架构的项目的时候需要修改的地方

1、需要修改的是.sln文件&#xff0c;即将里面的 Version改为12&#xff0c;其中的VS的版本改为2017 2、项目中后缀名为 .csproj中的代码改一下&#xff1a; 转载于:https://www.cnblogs.com/zhijianhao/p/10253572.html