欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 分页sql语句(结合aspnetpager)

程序员文章站 2023-11-18 22:31:52
数据库操作类:复制代码 代码如下:/// /// 取得总数 /// /// ...
数据库操作类:
复制代码 代码如下:

/// <summary>
/// 取得总数
/// </summary>
/// <returns></returns>
public string gettotal()
{
stringbuilder sb = new stringbuilder();
sb.append("select count(*) total from test");
datatable dt = dbhelper.executedt(sb.tostring());
return dt.rows[0][0].tostring();
}
/// <summary>
/// 根据当前页码,每页条数,取得相应数据。
/// </summary>
/// <param name="pagenum">每页显示条数</param>
/// <param name="currentpage">当前页码</param>
/// <returns></returns>
public datatable getpagesdata(int pagenum, int currentpage)
{
stringbuilder sb = new stringbuilder();
sb.append("select top " + pagenum + " * from test where ");
sb.append("id not in (select top " + pagenum * currentpage + " id from test)");
return dbhelper.executedt(sb.tostring());
}

前台:
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="sqlpager_default" %>
<%@ register assembly="aspnetpager" namespace="wuqi.webdiyer" tagprefix="webdiyer" %>

<!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">
<div>
<asp:gridview id="gvsql" runat="server">
</asp:gridview>
</div>
<div>
<webdiyer:aspnetpager id="aspnetpager1" runat="server" onpagechanged="aspnetpager1_pagechanged" pagesize="3">
</webdiyer:aspnetpager>
</div>
</form>
</body>
</html>

后台:
复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class sqlpager_default : system.web.ui.page
{
bll.test test = new bll.test();
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
aspnetpager1.recordcount = convert.toint32(test.gettotal());//此属性保存总记录数..
bind();
}
}
private void bind()
{
this.gvsql.datasource = test.getpagesdata(convert.toint32(aspnetpager1.pagesize), aspnetpager1.currentpageindex - 1);
this.gvsql.databind();
}
protected void aspnetpager1_pagechanged(object sender, eventargs e)
{
bind();
}
}