当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中Datalist使用数字分页的实现方法

asp.net中Datalist使用数字分页的实现方法

2017年12月12日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下: <%@ page language="c#" autoeventwireup="true" codefile="test(datalist数字分
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="test(datalist数字分页).aspx.cs" inherits="test_datalist数字分页_" %>
<!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>
<link href="css/css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:datalist id="datalist1" runat="server">
<itemtemplate>
<asp:label id="label1" runat="server" text='<%# eval("mid") %>'></asp:label>
</itemtemplate>
</asp:datalist>
</div>
<br />
<div id="pageinfo" runat="server" class="lpagebar"></div>
</form>
</body>
</html>

cs代码:
复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
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.xml.linq;
public partial class test_datalist数字分页_ : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
//comfunction cf = new comfunction();
//dataset ds = cf.databind("m_dizhi");
//this.pageinfo.innerhtml = pagenums.getpagenum(ds, datalist1, 12);
this.pageinfo.innerhtml = pagenums.getpagesql("m_dizhi", datalist1, 12);
}
}

pagenums.cs
复制代码 代码如下:

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.data.sqlclient;
/// <summary>
///pagenums 的摘要说明
/// </summary>
public class pagenums
{
/// </summary>
/// <param name="ds">dataset实例</param>
/// <param name="datalistname">datalist名称</param>
/// <param name="pagesize">分页大小</param>
public static string getpagenum(dataset ds, datalist datalistname, int pagesize)
{
pageddatasource objpds = new pageddatasource();
objpds.datasource = ds.tables[0].defaultview;
objpds.allowpaging = true;
int total = ds.tables[0].rows.count;
objpds.pagesize = pagesize;
int page;
if (httpcontext.current.request.querystring["page"] != null)
page = convert.toint32(httpcontext.current.request.querystring["page"]);
else
page = 1;
objpds.currentpageindex = page - 1;
datalistname.datasource = objpds;
datalistname.databind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize);//计算总页数
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页";
pagestr += page > 1 ? "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=1\">首页</a>;<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "<font color=\"#ff0000\">" + i + "</font>" : "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + next + "\">下一页</a><a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + allpage + "\">末页</a>" : " 下一页 末页";
return pagestr;
}

public static string getpagesql(string filename, datalist datalistname, int pagesize)
{
int page;
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (httpcontext.current.request.querystring["page"] != null)
page = convert.toint32(httpcontext.current.request.querystring["page"]);
else
page = 1;
if (page < 1) { page = 1; }
dataset ds = databindsql("exec pagesql '*',' from " + filename + " '," + pagesize + "," + page, filename);
datalistname.datasource = ds;
datalistname.databind();
int total = databind(filename);
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize);//计算总页数
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = page / 5 * 5;//中间页起始序号
//中间页终止序号
endcount = startcount+5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "<a>"+page + "/" + allpage + "页</a>";
pagestr += page > 1 ? "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=1\">首页</a><a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + pre + "\">上一页</a>" : "首页 上一页";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "<font color=\"#ff0000\">" + i + "</font>" : "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "<a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + next + "\">下一页</a><a href=\"" + httpcontext.current.request.currentexecutionfilepath + "?page=" + allpage + "\">末页</a>" : " 下一页 末页";
return pagestr;
}
private static int databind(string filename)
{
string sql = "select * from " + filename;
dbconnection dc = new dbconnection();
sqlconnection mycon = new sqlconnection(dc.connectionstring);
sqldataadapter mypter = new sqldataadapter(sql, mycon);
dataset ds = new dataset();
mycon.open();
mypter.fill(ds, filename);
mycon.close();
int total = ds.tables[0].rows.count;
return total;
}
private static dataset databindsql(string sql, string filename)
{
dbconnection dc = new dbconnection();
sqlconnection mycon = new sqlconnection(dc.connectionstring);
sqldataadapter mypter = new sqldataadapter(sql,mycon);
dataset ds = new dataset();
mycon.open();
mypter.fill(ds, filename);
mycon.close();
return ds;
}
}

存储过程pagesql
复制代码 代码如下:

create procedure pagesql
@sqlselect varchar(800) --select 后面 from 前面 的 字段 不用包含select
,@sqlfrom varchar(800) --from 后面 的 字段 包含from
,@countperpage int -- 每页数据行数
,@topage int --要转到的页码
as
begin

-- 根据每页数据行数 和 要转到的页码 得到 数据起止点
declare @start int
declare @end int
set @end = @countperpage * @topage
set @start = @countperpage * (@topage - 1) + 1

-- 临时表名称 可随机命名
declare @tmptable varchar(10)
set @tmptable ='#tmp'
declare @sqlstr varchar(800)
-- 创建数据源到临时表
select @sqlstr = 'select identity(int,1,1) as rowindex,'
select @sqlstr = @sqlstr + rtrim(@sqlselect) + ' into '+ @tmptable
select @sqlstr = @sqlstr + rtrim(@sqlfrom)
-- 查询临时表 得到所需要的数据
select @sqlstr = @sqlstr + ' '+'select '+ rtrim(@sqlselect) +' from ' + @tmptable
select @sqlstr = @sqlstr + ' where rowindex between ' + convert(char,@start) + " and " + convert(char,@end)
-- 删除临时表
select @sqlstr = @sqlstr + ' '+'drop table '+@tmptable
exec (@sqlstr)

end
go

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网