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

asp.net 源码保存 用程序分页

程序员文章站 2024-03-08 13:53:34
源码: 复制代码 代码如下: namespace alex { public class pagetools { /// /// 表名称 /...
源码:
复制代码 代码如下:

namespace alex
{
public class pagetools
{
/// <summary>
/// 表名称
/// </summary>
private string tablename;
public string tablename
{
get { return tablename; }
set { tablename = value; }
}
/// <summary>
/// 返回的列名
/// </summary>
private string returncolumns;
public string returncolumns
{
get { return returncolumns; }
set { returncolumns = value; }
}
/// <summary>
/// 条件
/// </summary>
private string where;
public string where
{
get { return where; }
set { where = value; }
}
/// <summary>
/// 排序列
/// </summary>
private string ordercolumnname;
public string ordercolumnname
{
get { return ordercolumnname; }
set { ordercolumnname = value; }
}
/// <summary>
/// 排序,true为降序
/// </summary>
private boolean orderdesc;
public boolean orderdesc
{
get { return orderdesc; }
set { orderdesc = value; }
}
/// <summary>
/// 主键
/// </summary>
private string keycolumnname;
public string keycolumnname
{
get { return keycolumnname; }
set { keycolumnname = value; }
}
/// <summary>
/// 页面大小
/// </summary>
private int32 pagesize;
public int32 pagesize
{
get { return pagesize; }
set { pagesize = value; }
}
/// <summary>
/// 第几页
/// </summary>
private int32 pageindex;
public int32 pageindex
{
get { return pageindex; }
set { pageindex = value; }
}
/// <summary>
/// sql参数
/// </summary>
private sqlparameter[] whereparams;
public sqlparameter[] whereparams
{
get { return whereparams; }
set { whereparams = value; }
}
/// <param name="tablename"></param>
/// <param name="returncolumns"></param>
/// <param name="where">条件</param>
/// <param name="ordercolumnname">排序列</param>
/// <param name="orderdesc">排序,true为降序</param>
/// <param name="keycolumnname">主键</param>
/// <param name="pagesize">页面大小</param>
/// <param name="pageindex">第几页</param>
/// <param name="whereparams">sql参数</param>
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];
}
}
/// <summary>
/// 获取数据的条数
/// </summary>
/// <param name="tablename"></param>
/// <param name="where"></param>
/// <param name="whereparams"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 获取第一页
/// </summary>
/// <param name="top"></param>
/// <param name="tablename"></param>
/// <param name="returncolumns"></param>
/// <param name="where"></param>
/// <param name="ordercolumnname"></param>
/// <param name="orderdesc"></param>
/// <param name="whereparams"></param>
/// <returns></returns>
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];
}
/// <summary>
/// 移除空参数
/// </summary>
/// <param name="whereparams"></param>
/// <returns></returns>
private sqlparameter[] checknull(sqlparameter[] paras)
{
if (paras == null)
{
return null;
}
list<sqlparameter> list = new list<sqlparameter>();
foreach (sqlparameter para in paras)
{
if (para != null) list.add(para);
}
return list.toarray();
}
}
}

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

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();
}

页面前台:

代码
复制代码 代码如下:

<%@ page language="c#" theme="msn_blue" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<%@ register assembly="smartgridview" namespace="bosswwebui.smartgridview" tagprefix="uc1" %>
<!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>
<script type="text/javascript">
function validate() {
var gocount=document.getelementbyid("gocount").value;
if(gocount=="")
{
alert("请先填写你要导航的页面");
return false;
}
var result;
if(isnan(gocount))
{
alert("请输入合法的数字");
document.getelementbyid("gocount").value="";
return false;
}
if(parseint(gocount)<=0|| parseint(gocount)><%=this.totalpages %>)
{
alert("数字溢出");
document.getelementbyid("gocount").value="";
return false;
}
return true;
}
</script>
<form id="form1" runat="server">
<div>
<table cellspacing="0" cellpadding="0" width="98%" align="center" border="0">
<tr id="trgrid" runat="server">
<td align="left">
○ 页次:<asp:label id="lblpage" runat="server" ></asp:label>/
<asp:label id="lblpagesum" runat="server"></asp:label>,共:<asp:label
id="lblrowscount" runat="server"></asp:label>条</td>
<td align="right">
<asp:linkbutton id="btnfirst" runat="server" oncommand="navigatetopage"
commandargument="first" commandname="pager" text="首 页">[首 页]</asp:linkbutton><asp:linkbutton
id="btnprev" runat="server" commandargument="prev" oncommand="navigatetopage"
commandname="pager" text="上一页">[上一页]</asp:linkbutton><asp:linkbutton id="btnnext"
runat="server" commandargument="next" oncommand="navigatetopage"
commandname="pager" text="下一页">[下一页]</asp:linkbutton><asp:linkbutton id="btnlast"
runat="server" commandargument="last" oncommand="navigatetopage"
commandname="pager" text="尾 页" >[尾 页]</asp:linkbutton>
<asp:textbox id=gocount runat="server" width="50px"></asp:textbox>
<asp:button id="gobtn" runat="server" text="go" onclientclick="return validate();" onclick="gobtnclick" /></td>
</tr>
</table>
<table cellspacing="0" cellpadding="0" width="98%" align="center" border="0">
<tr>
<td>
<uc1:smartgridview id="smartgridview1" skinid="blue" runat="server"
pagingstyle="default"
cssclassmouseover="grid_over" enableviewstate="false"
autogeneratecolumns="false"
width="100%" currentlanguage="">
<columns>
<asp:templatefield>
<headertemplate>
<asp:checkbox id="headcheckbox" runat="server" />
</headertemplate>
<itemtemplate>
<asp:checkbox id="checkitem" runat="server" __designer:wfdid="w1"></asp:checkbox>
</itemtemplate>
<headerstyle cssclass="tdcell" />
<itemstyle horizontalalign="center" width="5px" cssclass="tdcell" />
</asp:templatefield>
<asp:boundfield datafield="user_account" headertext="用户帐号" sortexpression="user_account">
<headerstyle cssclass="tdcell" />
<itemstyle horizontalalign="center" cssclass="tdcell" />
</asp:boundfield>
<asp:boundfield datafield="user_dspname" headertext="显示名称" sortexpression="user_dspname">
<headerstyle cssclass="tdcell" />
<itemstyle horizontalalign="center" cssclass="tdcell" />
</asp:boundfield>
<asp:boundfield datafield="user_dspengname" headertext="英文名" sortexpression="user_dspengname" >
<headerstyle cssclass="tdcell" />
<itemstyle cssclass="tdcell" horizontalalign="center" />
</asp:boundfield>
<asp:boundfield datafield="user_hrid" headertext="员工号" sortexpression="user_hrid" >
<headerstyle cssclass="tdcell" />
<itemstyle cssclass="tdcell" horizontalalign="center" />
</asp:boundfield>
<asp:boundfield datafield="user_email" headertext="邮件地址" sortexpression="user_email">
<headerstyle cssclass="tdcell" />
<itemstyle horizontalalign="center" cssclass="tdcell" />
</asp:boundfield>
<asp:boundfield datafield="user_title" headertext="职 称" sortexpression="user_title">
<headerstyle cssclass="tdcell" />
<itemstyle horizontalalign="center" cssclass="tdcell" />
</asp:boundfield>
<asp:boundfield datafield="user_tel" headertext="电 话" sortexpression="user_tel">
<headerstyle cssclass="tdcell" />
<itemstyle horizontalalign="center" cssclass="tdcell" />
</asp:boundfield>
<asp:boundfield datafield="user_active" headertext="是否有效" sortexpression="user_active">
<headerstyle cssclass="tdcell" />
<itemstyle horizontalalign="center" cssclass="tdcell" />
</asp:boundfield>
<asp:boundfield datafield="user_major" headertext="专业" sortexpression="user_major" >
<headerstyle cssclass="tdcell" horizontalalign="center" />
<itemstyle cssclass="tdcell" horizontalalign="center" />
</asp:boundfield>
</columns>
<headerstyle cssclass="thhead" />
<fixrowcol tableheight="" tablewidth="" />
<rowstyle cssclass="tdcell" />
</uc1:smartgridview>
</td></tr>
</table>
</div>
</form>
</body>
</html>