ASP.NET中DataList數字分頁代碼

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   使用   os   

好長時間沒發東西了, 之前一段時間一直在做別的東西, 最近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樣式寫在裡面了, 用的時候可以選選看看, 也可以自己改

 

下面是:


   

    

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.