asp.net 分页源码分页程序

作者:袖梨 2022-06-25

后面页面


public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
recordcount = page.retrievedatacount();
totalpages = (int)(math.ceiling((double)recordcount / (double)page.pagesize));
if (!ispostback)
{
binddata();
}
}
alex.pagetools page = new alex.pagetools("test", "*", "1=1", "user_account", false, "user_account", 10, 1, null);
int recordcount;
public int totalpages;

private void binddata()
{
int pageindex = smartgridview1.pageindex;
if(totalpages>0)
{
if(pageindex>totalpages-1)
{
pageindex=totalpages-1;
}
}
else
{
pageindex=0;
}
smartgridview1.pageindex=pageindex;
page.pageindex=pageindex+1;
smartgridview1.datasource = page.retrievepagerdata();
smartgridview1.databind();
if (this.smartgridview1.pageindex == 0)
{
this.btnfirst.enabled = false;
this.btnprev.enabled = false;
if (totalpages == 1)
{
this.btnlast.enabled = false;
this.btnnext.enabled = false;
}
}
else if (this.smartgridview1.pageindex == totalpages - 1)
{
this.btnlast.enabled = false;
this.btnnext.enabled = false;

 

this.lblpagesum.text = totalpages.tostring();
this.lblpage.text = (pageindex + 1).tostring();
this.lblrowscount.text = recordcount.tostring();
}
public void navigatetopage(object sender, commandeventargs e)
{
btnfirst.enabled = true;
btnprev.enabled = true;
btnnext.enabled = true;
btnlast.enabled = true;
string pageinfo = e.commandargument.tostring();
switch (pageinfo)
{
case "prev":
if (this.smartgridview1.pageindex > 0)
{
this.smartgridview1.pageindex -= 1;
}
break;
case "next":
if (this.smartgridview1.pageindex < (totalpages - 1))
{
this.smartgridview1.pageindex += 1;
}
break;
case "first":
this.smartgridview1.pageindex = 0;
break;
case "last":
this.smartgridview1.pageindex = totalpages - 1;
break;
}
if (this.smartgridview1.pageindex == 0)
{
btnfirst.enabled = false;
btnprev.enabled = false;
if (totalpages == 1)
{
btnlast.enabled = false;
btnnext.enabled = false;
}
}
else if (this.smartgridview1.pageindex == totalpages - 1)
{
btnlast.enabled = false;
btnnext.enabled = false;
}
binddata();
}
protected void gobtnclick(object sender, eventargs e)
{
int gopageindex = convert.toint32(this.gocount.text.trim());
if (gopageindex > 0)
{
if (gopageindex >= totalpages)
{
this.smartgridview1.pageindex = totalpages - 1;
}
else
{
this.smartgridview1.pageindex = gopageindex - 1;
}
}
else if(gopageindex <=0)
{
this.smartgridview1.pageindex = 0;
}
if (this.smartgridview1.pageindex == 0)
{
btnfirst.enabled = false;
btnprev.enabled = false;
btnlast.enabled = true;
btnnext.enabled = true;
if (totalpages == 1)
{
btnlast.enabled = false;
btnnext.enabled = false;
}
}
else if (this.smartgridview1.pageindex == totalpages - 1)
{
btnlast.enabled = false;
btnnext.enabled = false;
btnfirst.enabled = true;
btnprev.enabled = true;
}
else
{
btnlast.enabled = true;
btnfirst.enabled = true;
btnnext.enabled = true;
btnprev.enabled = true;
}
binddata();
}
前台页面
namespace alex
{
public class pagetools
{
///
/// 表名称
///

private string tablename;
public string tablename
{
get { return tablename; }
set { tablename = value; }
}
///
/// 返回的列名
///

private string returncolumns;
public string returncolumns
{
get { return returncolumns; }
set { returncolumns = value; }
}
///
/// 条件
///

private string where;
public string where
{
get { return where; }
set { where = value; }
}
///  
 

/// 排序列
///

private string ordercolumnname;
public string ordercolumnname
{
get { return ordercolumnname; }
set { ordercolumnname = value; }
}
///
/// 排序,true为降序
///

private boolean orderdesc;
public boolean orderdesc
{
get { return orderdesc; }
set { orderdesc = value; }
}
///
/// 主键
///

private string keycolumnname;
public string keycolumnname
{
get { return keycolumnname; }
set { keycolumnname = value; }
}
///
/// 页面大小
///

private int32 pagesize;
public int32 pagesize
{
get { return pagesize; }
set { pagesize = value; }
}
///
/// 第几页
///

private int32 pageindex;
public int32 pageindex
{
get { return pageindex; }
set { pageindex = value; }
}
///
/// sql参数
///

private sqlparameter[] whereparams;
public sqlparameter[] whereparams
{
get { return whereparams; }
set { whereparams = value; }
}
///
///
/// 条件
/// 排序列
/// 排序,true为降序
/// 主键
/// 页面大小
/// 第几页
/// sql参数
public pagetools(string tablename, string returncolumns, string where, string ordercolumnname, boolean orderdesc,
string keycolumnname, int32 pagesize, int32 pageindex, params sqlparameter[] whereparams)
{
this.tablename = tablename;
this.returncolumns = returncolumns;
this.where = where;
this.ordercolumnname = ordercolumnname;
this.orderdesc = orderdesc;
this.keycolumnname = keycolumnname;
this.pagesize = pagesize;
this.pageindex = pageindex;
this.whereparams = whereparams;
}
public pagetools() { }
public datatable retrievepagerdata()
{
if (pageindex == 1)
{
return retrievetopdata();
}
else
{
string sql = string.format("select top {0} {1} from {2} where {3} and {6} not in ( select top {7} {6} from {2} where {3} order by {4} {5} ) order by {4} {5}", pagesize, returncolumns, tablename, where, ordercolumnname, orderdesc == true ? "desc" : string.empty, keycolumnname, pagesize * (pageindex - 1));
return dbhelpersql.query(sql, checknull(whereparams)).tables[0];
}
}
///
/// 获取数据的条数 
 

///

///
///
///
///
public int32 retrievedatacount()
{
string sql = string.format("select count(*) from {0} where {1}" , tablename, where);
int32 result = (int32)dbhelpersql.getsingle(sql, checknull(whereparams));
return result;
}
///
/// 获取第一页
///

///
///
///
///
///
///
///
///
public datatable retrievetopdata()
{
string sql = string.format("select top {0} {1} from {2} where {3} order by {4} {5}", pagesize, returncolumns, tablename, where, ordercolumnname, orderdesc == true ? "desc" : string.empty);
return dbhelpersql.query(sql, whereparams).tables[0];
}
///
/// 移除空参数
///

///
///
private sqlparameter[] checknull(sqlparameter[] paras)
{
if (paras == null)
{
return null;
}
list list = new list();
foreach (sqlparameter para in paras)
{
if (para != null) list.add(para);
}
return list.toarray();
}
}
}

前台代码


<%@ page language="c#" theme="msn_blue" autoeventwireup="true" codefile="default.asp教程x.cs" inherits="_default" %>
<%@ register assembly="smartgridview" namespace="bosswwebui.smartgridview" tagprefix="uc1" %>
http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">













○ 页次:<asp教程:label id="lblpage" runat="server" >/
,共: id="lblrowscount" runat="server">

commandargument="first" commandname="pager" text="首 页">[首 页] id="btnprev" runat="server" commandargument="prev" oncommand="navigatetopage"
commandname="pager" text="上一页">[上一页]
runat="server" commandargument="next" oncommand="navigatetopage"
commandname="pager" text="下一页">[下一页]
runat="server" commandargument="last" oncommand="navigatetopage"
commandname="pager" text="尾 页" >[尾 页]







pagingstyle="default"
css教程classmouseo教程ver="grid_over" enableviewstate="false"
autogeneratecolumns="false"
width="100%" currentlanguage="">








css教程class="tdcell" />















































相关文章

精彩推荐