原帖及讨论:http://bbs.bccn.net/thread-207175-1-1.html
*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: hebingbing
*/ 时间: 2008-4-5 04:30 编程论坛首发
*/ 声明: 光看我这么晚了还在工作,转载这段文字应该保留吧……
*/ --------------------------------------------------------------------------------------
废话:清明节,同学回家的回家,旅游的旅游……我离家远是不可能回家了,旅游吧不感兴趣,觉得还不如看一场电影……呵呵,从小不喜欢旅游观光……
转入正题:大家都知道asp.net中的Gridview。datalist等都可以自定义分页,但是当你翻页的时候,数据表中的所有数据都会加载到内存,重新绑定,当然要是数据量小的话,这是可以的,我们也很乐意用,原因简单因为方便,但是要是数据量是999999999999……,在信息爆炸的这个时代海量数据是经常的时,那么这些控件自带的分页就显得有些……
解决这个问题办法就是自己动手……不多废话了,看代码:
1.首先我是用存储过程来解决的,要弄懂这个问题,首先要从存储过程下手,代码如下:

Code
1
CREATE proc getdataset
2
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
3
@TableName Varchar(30), --搜索的表名
4
@SelectWhere Varchar(500)='',--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
5
@SelectOrderId Varchar(20),--表主键字段名。比如:id
6
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
7
@intPageNo int=1, --页号
8
@intPageSize int=10 ,--每页显示数
9
@RecordCount int OUTPUT --总记录数(存储过程输出参数)
10
as
11
12
declare @TmpSelect NVarchar(600)
13
declare @Tmp NVarchar(600)
14
15
set nocount on--关闭计数
16
17
set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhere
18
19
execute sp_executesql
20
@TmpSelect, --执行上面的sql语句
21
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
22
@RecordCount OUTPUT
23
24
if (@RecordCount = 0) --如果没有贴子,则返回零
25
return 0
26
27
/**//*判断页数是否正确*/
28
if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
29
return (-1)
30
set nocount off--打开计数
31
if @SelectWhere != ''
32
begin
33
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
34
end
35
else
36
begin
37
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
38
end
39
execute sp_executesql @TmpSelect
40
return(@@rowcount)
41
GO
42
其实代码也很简单,学编程的人基本上都是懂数据库的,这个存储过程估计不是问题。
其他的代码我都做了解释,有颜色的那段我没有解释,我在这里解释一下。其实也很简单,大家来看:
select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+'
这段代码的执行结果是什么了,是不是当前页前面的主键的集合啊,现在我们从所有的表中选出主键的值不在这个结果的之内的pagesize个记录不就是当前页的内容了吗?
2.aspx页面就不用再将了吧?我这里将代码写上:

页面代码
1
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="aa.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
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="180px" Width="867px">
13
<Columns>
14
<asp:BoundField DataField="job_id" HeaderText="job_id" />
15
<asp:BoundField DataField="job_desc" HeaderText="job_desc" />
16
<asp:BoundField DataField="max_lvl" HeaderText="max_lxl" />
17
</Columns>
18
</asp:GridView>
19
20
</div>
21
<asp:HyperLink ID="hylfirst" runat="server">首页</asp:HyperLink>
22
23
<asp:HyperLink ID="hylprev" runat="server">上一页</asp:HyperLink>
24
25
<asp:HyperLink ID="hylnext" runat="server">下一页</asp:HyperLink>
26
<asp:HyperLink ID="hylend" runat="server">尾页</asp:HyperLink>
27
第<asp:Label ID="lbRow" runat="server" Text="Label"></asp:Label>页,
28
共<asp:Label ID="lbpage" runat="server" Text="Label"></asp:Label>页,共<asp:Label
29
ID="lbRecord" runat="server" Text="Label"></asp:Label>条记录,转到<asp:TextBox ID="txtlink"
30
runat="server" Width="29px"></asp:TextBox>
31
页<asp:LinkButton ID="link" runat="server" OnClick="link_Click" TabIndex="1">转到</asp:LinkButton>
32
</form>
33
</body>
34
</html>
3.cs页面其实也每页什么好讲的,也就是一些常用的代码罢了……我把代码加上,大家看看,要是有疑问的可以回复我再解释:

代码页
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
13
public partial class _Default : System.Web.UI.Page
14

{
15
protected void Page_Load(object sender, EventArgs e)
16
{
17
18
this.bind();
19
20
}
21
22
protected void link_Click(object sender, EventArgs e)
23
{
24
int page = Convert.ToInt32(txtlink.Text);
25
Response.Redirect("aa.aspx?CurrentPage="+page+"");
26
}
27
public void bind()
28
{
29
int sumPage;
30
int pageNo = 1;
31
int pageSize = 3;
32
if (Request.QueryString["CurrentPage"] == null)
33
{
34
pageNo = 1;
35
}
36
else
37
{
38
pageNo = Int32.Parse(Request.QueryString["CurrentPage"]);
39
}
40
41
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConStr"]);
42
SqlDataAdapter da = new SqlDataAdapter();
43
da.SelectCommand = new SqlCommand();
44
da.SelectCommand.Connection = conn;
45
da.SelectCommand.CommandText = "getdataset";
46
da.SelectCommand.CommandType = CommandType.StoredProcedure;
47
da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = "job_id,job_desc,max_lvl";
48
da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30).Value = "jobs";
49
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where d=1";
50
da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = "job_id";
51
da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "order by min_lvl asc";
52
da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = pageNo;
53
da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = pageSize;
54
da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
55
da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
56
DataSet ds = new DataSet();
57
da.Fill(ds, "jobs");
58
GridView1.DataSource = ds;
59
GridView1.DataBind();
60
Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value; //求出总记录数,该值是output出来的值
61
Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value; //求出当前页中的记录数,在最后一页不等于pagesize,
62
lbRecord.Text = RecordCount.ToString();
63
lbRow.Text = pageNo.ToString();
64
sumPage = (Int32)RecordCount / pageSize;
65
if (RecordCount % pageSize > 0)
66
{
67
sumPage = sumPage + 1;
68
}
69
lbpage.Text = sumPage.ToString();
70
if (pageNo > 1)
71
{
72
hylfirst.NavigateUrl = "aa.aspx?CurrentPage=1";
73
hylprev.NavigateUrl = string.Concat("aa.aspx?CurrentPage=", "", pageNo - 1);
74
}
75
else
76
{
77
hylprev.NavigateUrl = "";
78
hylfirst.NavigateUrl = "";
79
hylfirst.Visible = false;
80
hylprev.Visible = false;
81
}
82
if (pageNo < sumPage)
83
{
84
hylend.NavigateUrl = string.Concat("aa.aspx?CurrentPage=", "", sumPage);
85
hylnext.NavigateUrl = string.Concat("aa.aspx?CurrentPage=", "", pageNo + 1);
86
}
87
else
88
{
89
hylnext.NavigateUrl = "";
90
hylend.NavigateUrl = "";
91
hylend.Visible = false;
92
hylnext.Visible = false;
93
}
94
95
}
96
}