ASP.NET中DataList数字分页代码

ASP.NET中DataList数字分页代码 转 荐

 

好长时间没发东西了, 之前一段时间一直在做别的东西, 最近java实验室要开搞系统哈哈, 重新学习.net

这个分页困扰了我这个菜鸟好久阿, 搞了好久终于找到这段代码, 还是数字分页, 试了成功了~还不会分页的朋友看到这个就偷笑吧~~

代码转自http://hi.baidu.com/honfei css样式取自吴旗娃aspnetpager

效果:

共1页       首页 上一页  1 2 3 4 5 6 7 8 9 10 ....下一页 末页
 

调用页面(Aspx):

在要显示分页导航的地方加个DIV标签:<div id="PageInfo" runat="server"></div>

调用页的代码(CS):

DataSet ds = db.getDs(sql);
this.PageInfo.InnerHtml = PageNums.GetPageNum(ds,DataList1,12); //传入DataSet,DataList名称和分页大小

PageNums实现分页类(PageNums.cs):下面附

 

为了能配合上吴旗娃的分页样式, 做了细微的改动, 在此贴上全部的代码, 方便大家使用

 

Default2.aspx.cs

 

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;
using System.Data;
using System.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
protected SqlConnection conn; //添加数据库的操作对象
protected SqlDataAdapter da;
protected DataSet ds;
protected SqlCommand comm;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=localhost;Initial Catalog=nd_data;User ID=sa;Password=aaaaaa");//取连接字符串,建立连接
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select name,id from xs Order by id,name DESC ", conn);
ds = new DataSet();
try
{
conn.Open();
da.Fill(ds, "abs");
conn.Close();
}
catch (SqlException e1)
{
Response.Write(e1.ToString());
}


this.PageInfo.InnerHtml = PageNums.GetPageNum(ds, DataList1,5); //传入DataSet,DataList名称和分页大小
}
}

 

 

 

Default2.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>
<style type="text/css">
.paginator { font: 11px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}
.paginator a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px}
.paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.paginator .cpb {padding: 1px 6px;font-weight: bold; font-size: 13px;border:none}
.paginator a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}

.anpager { font: 11px Arial, Helvetica, sans-serif;margin:5px 4px 0 0;padding:4px 5px 0;}
.anpager .cpb {background:#1F3A87;border:1px solid #CCCCCC;color:#FFFFFF;font-weight:bold;margin:0;padding:4px 5px 1px;}
.anpager a {background:#FFFFFF;border:1px solid #CCCCCC;color:#1F3A87;margin: 0;padding:4px 5px 1px;text-decoration:none}
.anpager a:hover{background:#1F3A87;border:1px solid #1F3A87;color:#FFFFFF;}

.pages { color: #999; }
.pages a, .pages .cpb { text-decoration:none;float: left; padding: 0 5px; border: 1px solid #ddd;background: #ffff;margin:0 2px; font-size:11px; color:#000;}
.pages a:hover { background-color: #E61636; color:#fff;border:1px solid #E61636; text-decoration:none;}
.pages .cpb { font-weight: bold; color: #fff; background: #E61636; border:1px solid #E61636;}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="lbNwes" runat="server" Text=‘<%#Eval("id")%>‘></asp:Label >
&nbsp;
<asp:Label ID="lbTime" runat="server" Text=‘<%#Eval("name")%>‘></asp:Label>
</ItemTemplate>
</asp:DataList>
<br />
<div id="PageInfo" runat="server" class="anpager"></div>
</form>
</body>
</html>

 

 

 

PageNums.cs

 

using System;
using System.Data;
using System.Configuration;
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;

/// <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 = "<a >"+"共" + allpage + "页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首页</a>&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一页</a>" : "<a>首页</a>" + "&nbsp;&nbsp;" + "<a>上一页</a>";
//中间页处理,这个增加时间复杂度,减小空间复杂度
for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? "&nbsp;&nbsp;" +"<a class=\"cpb\">"+ i + "</a>" : "&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "\">" + i + "</a>";
}
pagestr += page != allpage ? "&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "\">下一页</a>&nbsp;&nbsp;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末页</a>" : "&nbsp;&nbsp;" + "<a >下一页</a>" + "&nbsp;&nbsp;" + "<a >末页</a>";
return pagestr;
}
}

 

 

3个css样式写在里面了, 用的时候可以选选看看, 也可以自己改

 

下面是效果图:


   

    

    

ASP.NET中DataList数字分页代码,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。