asp.net中Datalist使用數(shù)字分頁(yè)的實(shí)現(xiàn)方法
更新時(shí)間:2010年10月22日 16:28:39 作者:
asp.net下Datalist使用數(shù)字分頁(yè)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test(Datalist數(shù)字分頁(yè)).aspx.cs" Inherits="Test_Datalist數(shù)字分頁(yè)_" %>
<!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>無(wú)標(biāo)題頁(yè)</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代碼:
復(fù)制代碼 代碼如下:
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數(shù)字分頁(yè)_ : 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
復(fù)制代碼 代碼如下:
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 的摘要說(shuō)明
/// </summary>
public class PageNums
{
/// </summary>
/// <param name="ds">DataSet實(shí)例</param>
/// <param name="datalistname">DataList名稱(chēng)</param>
/// <param name="pagesize">分頁(yè)大小</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; }
//計(jì)算總頁(yè)數(shù)
if (pagesize != 0)
{
allpage = (total / pagesize);//計(jì)算總頁(yè)數(shù)
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;//中間頁(yè)起始序號(hào)
//中間頁(yè)終止序號(hào)
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //為了避免輸出的時(shí)候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號(hào)1開(kāi)始
if (allpage < endcount) { endcount = allpage; } //頁(yè)碼+5的可能性就會(huì)產(chǎn)生最終輸出序號(hào)大于總頁(yè)碼,那么就要將其控制在頁(yè)碼數(shù)之內(nèi)
pagestr = "共" + allpage + "頁(yè)";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首頁(yè)</a>;<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一頁(yè)</a>" : "首頁(yè) 上一頁(yè)";
//中間頁(yè)處理,這個(gè)增加時(shí)間復(fù)雜度,減小空間復(fù)雜度
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 + "\">下一頁(yè)</a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末頁(yè)</a>" : " 下一頁(yè) 末頁(yè)";
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);
//計(jì)算總頁(yè)數(shù)
if (pagesize != 0)
{
allpage = (total / pagesize);//計(jì)算總頁(yè)數(shù)
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = page / 5 * 5;//中間頁(yè)起始序號(hào)
//中間頁(yè)終止序號(hào)
endcount = startcount+5;
if (startcount < 1) { startcount = 1; } //為了避免輸出的時(shí)候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號(hào)1開(kāi)始
if (allpage < endcount) { endcount = allpage; } //頁(yè)碼+5的可能性就會(huì)產(chǎn)生最終輸出序號(hào)大于總頁(yè)碼,那么就要將其控制在頁(yè)碼數(shù)之內(nèi)
pagestr = "<a>"+page + "/" + allpage + "頁(yè)</a>";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1\">首頁(yè)</a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "\">上一頁(yè)</a>" : "首頁(yè) 上一頁(yè)";
//中間頁(yè)處理,這個(gè)增加時(shí)間復(fù)雜度,減小空間復(fù)雜度
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 + "\">下一頁(yè)</a><a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "\">末頁(yè)</a>" : " 下一頁(yè) 末頁(yè)";
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;
}
}
存儲(chǔ)過(guò)程pagesql
復(fù)制代碼 代碼如下:
CREATE PROCEDURE pagesql
@sqlSelect varchar(800) --SELECT 后面 FROM 前面 的 字段 不用包含SELECT
,@sqlFrom varchar(800) --FROM 后面 的 字段 包含F(xiàn)ROM
,@countPerPage int -- 每頁(yè)數(shù)據(jù)行數(shù)
,@toPage int --要轉(zhuǎn)到的頁(yè)碼
AS
BEGIN
-- 根據(jù)每頁(yè)數(shù)據(jù)行數(shù) 和 要轉(zhuǎn)到的頁(yè)碼 得到 數(shù)據(jù)起止點(diǎn)
Declare @start int
Declare @end int
set @end = @countPerPage * @toPage
set @start = @countPerPage * (@toPage - 1) + 1
-- 臨時(shí)表名稱(chēng) 可隨機(jī)命名
Declare @tmpTable varchar(10)
SET @tmpTable ='#tmp'
Declare @sqlStr varchar(800)
-- 創(chuàng)建數(shù)據(jù)源到臨時(shí)表
SELECT @sqlStr = 'SELECT Identity(int,1,1) AS RowIndex,'
SELECT @sqlStr = @sqlStr + rtrim(@sqlSelect) + ' INTO '+ @tmpTable
SELECT @sqlStr = @sqlStr + rtrim(@sqlFrom)
-- 查詢(xún)臨時(shí)表 得到所需要的數(shù)據(jù)
SELECT @sqlStr = @sqlStr + ' '+'SELECT '+ rtrim(@sqlSelect) +' FROM ' + @tmpTable
SELECT @sqlStr = @sqlStr + ' WHERE RowIndex BETWEEN ' + Convert(char,@start) + " AND " + Convert(char,@end)
-- 刪除臨時(shí)表
SELECT @sqlStr = @sqlStr + ' '+'DROP TABLE '+@tmpTable
EXEC (@sqlStr)
END
GO
您可能感興趣的文章:
- repeater分頁(yè) 內(nèi)容顯示
- asp.net repeater手寫(xiě)分頁(yè)實(shí)例代碼
- asp.net Datalist控件實(shí)現(xiàn)分頁(yè)功能
- asp.net Repeater之非常好的數(shù)據(jù)分頁(yè)
- asp.net 通過(guò)aspnetpager為DataList分頁(yè)
- asp.net下Repeater使用 AspNetPager分頁(yè)控件
- asp.net中讓Repeater和GridView支持DataPager分頁(yè)
- asp.net Repeater分頁(yè)實(shí)例(PageDataSource的使用)
- Repeater控件與PagedDataSource結(jié)合實(shí)現(xiàn)分頁(yè)功能
- 在ASP.NET 2.0中操作數(shù)據(jù)之四十一:DataList和Repeater數(shù)據(jù)分頁(yè)
相關(guān)文章
asp.net類(lèi)庫(kù)中添加WebService引用出現(xiàn)問(wèn)題解決方法
在Web項(xiàng)目?jī)?nèi)添加WebService的引用是件很簡(jiǎn)單的事情,不過(guò)對(duì)于一些新手朋友來(lái)說(shuō),就沒(méi)有那么簡(jiǎn)單了,因?yàn)樵谔砑拥倪^(guò)程中總會(huì)遇到一些困難,接下來(lái)詳細(xì)介紹如何解決,感興趣的你可不要錯(cuò)過(guò)了啊2013-02-02在ASP.NET中連接SQL Server的簡(jiǎn)單方法
在ASP.NET中訪(fǎng)問(wèn)SQL Server數(shù)據(jù)庫(kù)有兩種方法,它們是System.Data.OleDb和System.Data.SqlClient.下面這段程序以System.Data.SqlClient為例訪(fǎng)問(wèn)本地?cái)?shù)據(jù)庫(kù)服務(wù)器.2013-04-04刪除特殊字符和限定用戶(hù)輸入長(zhǎng)度的示例代碼
在填寫(xiě)注冊(cè)表單時(shí)工程師妹都會(huì)考慮到刪除特殊字符和限定用戶(hù)輸入長(zhǎng)度等等,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下2013-10-10ASP.NET中GridView和Repeater重復(fù)數(shù)據(jù)如何合并
這篇文章主要介紹了ASP.NET中GridView和Repeater重復(fù)數(shù)據(jù)合并的方法,感興趣的小伙伴們可以參考一下2016-08-08"PageMethods未定義"或"對(duì)象不支持此屬性或方法"解決方法分享
PageMethods未定義或?qū)ο蟛恢С执藢傩曰蚍椒ń鉀Q方法,需要的朋友可以參考下。2010-12-12