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

asp.net url分页类代码

程序员文章站 2024-03-09 13:18:23
复制代码 代码如下: using system; using system.data; using system.configuration; using system.w...
复制代码 代码如下:

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

/// <summary>
///cutpage 的摘要说明
/// </summary>
public class cutpage
{
public cutpage()
{
//
//todo: 在此处添加构造函数逻辑
//
}
#region 私有成员变量
private string url; //分页时所用到的页面地址
private int count; //数据总条数
private int pagecount; //总页数
private int curretpage; //当前页数
private string id; //接收传递参数的值
private int startid; //数据循环的开始值
private int endid; //数据结束的值
private datatable dt; //数据dt值
private int datacount; //每页现实的数据条数
private string cssurl; //cssurl地址
#endregion
#region 公共变量
/// <summary>
/// url地址
/// </summary>
public string url
{
get
{
return url;
}
set
{
this.url = value;
}
}
/// <summary>
/// 数据总条数
/// </summary>
public int count
{
get
{
return count;
}
set
{
this.count = value;
}
}
/// <summary>
/// 数据总页数(该字段只读)
/// </summary>
public int pagecount
{
get
{
if (count % datacount == 0)
{
return convert.toint32(count / datacount);
}
else
{
return convert.toint32(count / datacount) + 1;
}
}
}
/// <summary>
/// 分页样式表url
/// </summary>
public string cssurl
{
get { return cssurl; }
set { this.cssurl = value; }
}
/// <summary>
/// 当前页数
/// </summary>
public int curretpage
{
get { return this.curretpage; }
set { this.curretpage = value; }
}
/// <summary>
/// 传递的参数值
/// </summary>
public string id
{
get { return this.id; }
set { this.id = value; }
}
/// <summary>
/// 数据开始值(该字段只读)
/// </summary>
public int startid
{
get
{
if (curretpage == 1)
{
return 0;
}
else
{
return (curretpage-1) * datacount;
}
}
}
/// <summary>
/// 数据结束的值(该字段只读)
/// </summary>
public int endid
{
get
{
if (curretpage == pagecount)
{
return this.dt.rows.count;
}
else
{
return (curretpage) * datacount;
}
}
}
/// <summary>
/// 用于分页的数据源
/// </summary>
public datatable dt
{
get { return this.dt; }
set { this.dt = value; }
}
/// <summary>
/// 每页显示的数据条数
/// </summary>
public int datacount
{
get { return this.datacount; }
set { this.datacount = value; }
}
#endregion
/// <summary>
/// 分页方法(生成分页代码的过程)
/// </summary>
/// <param name="pageinfo">literal控件 </param>
public void cutpagemethod(literal pt)
{
stringbuilder orderinfosb = new stringbuilder();
orderinfosb.append("<span style=\"width:1000px\"><tr id=\"pagination-digg\"><th style=\"width:180px\">");
orderinfosb.append("当前" + curretpage + "/" + pagecount + "页 共" + count + "条数据");
orderinfosb.append("</th><th class=\"previous-off\" style=\"align:right\">");
if (convert.toint32(this.id) == 1)
{
orderinfosb.append("<a href='#' disabled='flase'>首页</a>");
}
else
{
orderinfosb.append("<a href='" + url + "?id=1'>首页</a>");
}
orderinfosb.append("</th><th>");
if (convert.toint32(this.id )== 1 || this.id==null || this.id==string.empty)
{
orderinfosb.append("<a href='#'disabled='flase'>上一页</a>");
}
else
{
orderinfosb.append("<a href='" + url + "?id="+convert.tostring(curretpage-1)+"'>上一页</a>");
}
if (convert.toint32(this.id) < pagecount)
{
orderinfosb.append("<a href='" + url + "?id=" +convert.tostring(curretpage + 1) + "'>下一页</a>");

}
else
{
orderinfosb.append("<a href='#'disabled='flase'>下一页</a>");
}
orderinfosb.append("</th><th>");
if (convert.toint32(this.id) == pagecount)
{
orderinfosb.append("<a href='#' disabled='flase'>末页</a>");
}
else
{
orderinfosb.append("<a href='" + url + "?id="+convert.tostring(pagecount)+"'>末页</a>");

}
orderinfosb.append("</th></tr></span>");
pt.text = orderinfosb.tostring();

}
}

样式大家可以自己添,老实说没什么技术含量。
前台代码:
代码
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="cutpagetest.aspx.cs" inherits="cutpagetest" enableviewstate="false"%>

<!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" id="had">
<%--<link href="style/base.css" type="text/css" />--%>

<title>无标题页</title>
<style type="text/css">
a
{
text-decoration: none;

}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:literal id="liinfo" runat="server"></asp:literal>
<asp:literal id="lt" runat="server"></asp:literal>
<a href="#"
</div>
</form>
</body>
</html>

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

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

public partial class cutpagetest : system.web.ui.page
{
cutpage cp=new cutpage();
string id;
protected void page_load(object sender, eventargs e)
{
showpagedata(dbhelpersql.queryredt("select * from test",globalconfig.tcclinedbhelper), "cutpagetest.aspx");
//response.write();

}
public void showpagedata(datatable dt,string url)
{
//cp.cssurl = "style/pagecut.css";
id = request.querystring["id"];
cp.id = id;
cp.dt = dt;
had.innerhtml = "<link href=\"css/text\" src='" + cp.cssurl + "'/>";
if (id == null || id == "")
{
cp.curretpage = 1;
}
else
{
cp.curretpage = convert.toint32(id);
}
cp.url = url;
cp.datacount = 2;
cp.count = cp.dt.rows.count;
cp.cutpagemethod(lt);
for (int i = cp.startid; i < cp.endid; i++)
{
liinfo.text += cp.dt.rows[i][1].tostring() + "<br/>";
}
}
}