程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net中Datalist使用數字分頁的實現方法

asp.net中Datalist使用數字分頁的實現方法

編輯:ASP.NET基礎
復制代碼 代碼如下:
<%@ 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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved