*/
*/ 出自 编程中国
*/ 作者 hebingbing
*/ 时间 编程论坛首发
*/ 声明 光看我这么晚了还在工作转载这段文字应该保留吧……
*/
废话清明节同学回家的回家旅游的旅游……我离家远是不可能回家了旅游吧不感兴趣觉得还不如看一场电影……呵呵从小不喜欢旅游观光……
转入正题大家都知道中的Gridviewdatalist等都可以自定义分页但是当你翻页的时候数据表中的所有数据都会加载到内存重新绑定当然要是数据量小的话这是可以的我们也很乐意用原因简单因为方便但是要是数据量是……在信息爆炸的这个时代海量数据是经常的时那么这些控件自带的分页就显得有些……
解决这个问题办法就是自己动手……不多废话了看代码
首先我是用存储过程来解决的要弄懂这个问题首先要从存储过程下手代码如下
CREATE proc getdataset
@TableList Varchar()=*搜索表的字段比如iddatatimejob用逗号隔开
@TableName Varchar() 搜索的表名
@SelectWhere Varchar()=搜索条件这里不用写where比如job=teacherand class=
@SelectOrderId Varchar()表主键字段名比如id
@SelectOrder Varchar()= 排序可以使用多字段排序但主键字段必需在最前面也可以不写比如order by class asc
@intPageNo int= 页号
@intPageSize int= 每页显示数
@RecordCount int OUTPUT 总记录数(存储过程输出参数)
as
declare @TmpSelect NVarchar()
declare @Tmp NVarchar()
set nocount on关闭计数
set @TmpSelect = select @RecordCount = count(*) from +@TableName+ +@SelectWhere
execute sp_executesql
@TmpSelect 执行上面的sql语句
N@RecordCount int OUTPUT 执行输出数据的sql语句output出总记录数
@RecordCount OUTPUT
if (@RecordCount = ) 如果没有贴子则返回零
return
/*判断页数是否正确*/
if (@intPageNo ) * @intPageSize > @RecordCount 页号大于总页数返回错误
return ()
set nocount off打开计数
if @SelectWhere !=
begin
set @TmpSelect = select top +str(@intPageSize)+ +@TableList+ from +@TableName+ where +@SelectOrderId+ not in(select top +str((@intPageNo)*@intPageSize)+ +@SelectOrderId+ from +@TableName+ +@SelectWhere + +@SelectOrder+) and +@SelectWhere + +@SelectOrder
end
else
begin
set @TmpSelect = select top +str(@intPageSize)+ +@TableList+ from +@TableName+ where +@SelectOrderId+ not in(select top +str((@intPageNo)*@intPageSize)+ +@SelectOrderId+ from +@TableName+ +@SelectOrder+) +@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO
其实代码也很简单学编程的人基本上都是懂数据库的这个存储过程估计不是问题
其他的代码我都做了解释有颜色的那段我没有解释我在这里解释一下其实也很简单大家来看
select top +str((@intPageNo)*@intPageSize)+ +@SelectOrderId+ from +@TableName+ +@SelectWhere + +@SelectOrder+
这段代码的执行结果是什么了是不是当前页前面的主键的集合啊现在我们从所有的表中选出主键的值不在这个结果的之内的pagesize个记录不就是当前页的内容了吗?
aspx页面就不用再将了吧?我这里将代码写上
<%@ Page Language=C# AutoEventWireup=true CodeFile=aaaspxcs Inherits=_Default %>
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns= >
<head runat=server>
<title>无标题页</title>
</head>
<body>
<form id=form runat=server>
<div>
<asp:GridView ID=GridView runat=server AutoGenerateColumns=False Height=px Width=px>
<Columns>
<asp:BoundField DataField=job_id HeaderText=job_id />
<asp:BoundField DataField=job_desc HeaderText=job_desc />
<asp:BoundField DataField=max_lvl HeaderText=max_lxl />
</Columns>
</asp:GridView>
</div>
<asp:HyperLink ID=hylfirst runat=server>首页</asp:HyperLink>
<asp:HyperLink ID=hylprev runat=server>上一页</asp:HyperLink>
<asp:HyperLink ID=hylnext runat=server>下一页</asp:HyperLink>
<asp:HyperLink ID=hylend runat=server>尾页</asp:HyperLink>
第<asp:Label ID=lbRow runat=server Text=Label></asp:Label>页
共<asp:Label ID=lbpage runat=server Text=Label></asp:Label>页共<asp:Label
ID=lbRecord runat=server Text=Label></asp:Label>条记录转到<asp:TextBox ID=txtlink
runat=server Width=px></asp:TextBox>
页<asp:LinkButton ID=link runat=server OnClick=link_Click TabIndex=>转到</asp:LinkButton>
</form>
</body>
</html>
cs页面其实也每页什么好讲的也就是一些常用的代码罢了……我把代码加上大家看看要是有疑问的可以回复我再解释
using System;
using SystemData;
using SystemConfiguration;
using SystemCollections;
using SystemWeb;
using SystemWebSecurity;
using SystemWebUI;
using SystemWebUIWebControls;
using SystemWebUIWebControlsWebParts;
using SystemWebUIHtmlControls;
using SystemDataSqlClient;
public partial class _Default : SystemWebUIPage
{
protected void Page_Load(object sender EventArgs e)
{
thisbind()
}
protected void link_Click(object sender EventArgs e)
{
int page = ConvertToInt(txtlinkText)
ResponseRedirect(aaaspx?CurrentPage=+page+)
}
public void bind()
{
int sumPage;
int pageNo = ;
int pageSize = ;
if (RequestQueryString[CurrentPage] == null)
{
pageNo = ;
}
else
{
pageNo = IntParse(RequestQueryString[CurrentPage])
}
SqlConnection conn = new SqlConnection(ConfigurationManagerAppSettings[ConStr])
SqlDataAdapter da = new SqlDataAdapter()
daSelectCommand = new SqlCommand()
daSelectCommandConnection = conn;
daSelectCommandCommandText = getdataset;
daSelectCommandCommandType = CommandTypeStoredProcedure;
daSelectCommandParametersAdd(@TableList SqlDbTypeVarChar )Value = job_idjob_descmax_lvl;
daSelectCommandParametersAdd(@TableName SqlDbTypeVarChar )Value = jobs;
//daSelectCommandParametersAdd(@SelectWhere SqlDbTypeVarChar )Value = where d=;
daSelectCommandParametersAdd(@SelectOrderId SqlDbTypeVarChar )Value = job_id;
daSelectCommandParametersAdd(@SelectOrder SqlDbTypeVarChar )Value = order by min_lvl asc;
daSelectCommandParametersAdd(@intPageNo SqlDbTypeInt)Value = pageNo;
daSelectCommandParametersAdd(@intPageSize SqlDbTypeInt)Value = pageSize;
daSelectCommandParametersAdd(@RecordCount SqlDbTypeInt)Direction = ParameterDirectionOutput;
daSelectCommandParametersAdd(RowCount SqlDbTypeInt)Direction = ParameterDirectionReturnValue;
DataSet ds = new DataSet()
daFill(ds jobs)
GridViewDataSource = ds;
GridViewDataBind()
Int RecordCount = (Int)daSelectCommandParameters[@RecordCount]Value; //求出总记录数该值是output出来的值
Int RowCount = (Int)daSelectCommandParameters[RowCount]Value; //求出当前页中的记录数在最后一页不等于pagesize
lbRecordText = RecordCountToString()
lbRowText = pageNoToString()
sumPage = (Int)RecordCount / pageSize;
if (RecordCount % pageSize > )
{
sumPage = sumPage + ;
}
lbpageText = sumPageToString()
if (pageNo > )
{
hylfirstNavigateUrl = aaaspx?CurrentPage=;
hylprevNavigateUrl = stringConcat(aaaspx?CurrentPage= pageNo )
}
else
{
hylprevNavigateUrl = ;
hylfirstNavigateUrl = ;
hylfirstVisible = false;
hylprevVisible = false;
}
if (pageNo < sumPage)
{
hylendNavigateUrl = stringConcat(aaaspx?CurrentPage= sumPage)
hylnextNavigateUrl = stringConcat(aaaspx?CurrentPage= pageNo + )
}
else
{
hylnextNavigateUrl = ;
hylendNavigateUrl = ;
hylendVisible = false;
hylnextVisible = false;
}
}
}
就这样吧要是大家有疑问回帖我们再讨论在研究……