首先聲明以下幾點:
1、也許講解有點初級,希望高手不要“噴”我,因為我知道並不是每一個人都是高手,我也怕高手們說我裝13,小生不才;
2、如有什麼不對的地方,還希望大家指出,一定虛心學習;
3、本文屬於作者原創,尊重他人勞動成果,轉載請注明作者,謝謝。
下面開講:
首先說下思路,寫一個存儲過程,我也找了一個存儲過程,不過不是我寫的,出處:http://www.cnblogs.com/zhongweiv/archive/2011/10/31/JqueryPagination.html 這是一個通過jqurey+ajax實現無刷新分頁的例子,應該也不錯,因為有的時候要無刷新嘛,我用的存儲過程就是從那裡copy來的,但是我看了58.com還有一些其他的一些網站信息分頁都不會通過無刷新的,所以我特地的寫了一個有刷新的分頁的方法,好了,先寫一個存儲過程,然後就是通過div+css來布局點頁碼傳到該頁面的參數的值了,思路是通過用StringBuilder類後台布局div以實現動態的頁碼,然後通過頁碼調用存儲過程,得到相應的信息,最後就是一些細節了,不斷的運行調試找出BUG並改正...
一、首先看下頁面前台代碼
復制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page.aspx.cs" Inherits="Jquery.Page"%>
<!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>Tandy Tang有刷新分頁...</title>
<style type="text/css">
/*分頁部分 start*/
ul
{
margin:0px;
padding:0px;
}
.page
{
width:700px;
background-color:#ffffff;
height:50px;
margin-top:15px;
}
.page ul li
{
float:left;
display:block;
width:28px;
height:23px;
text-align:center;
margin-left:1px;
vertical-align:middle;
border-style:solid;
border-width:1px;
border-color:#b1add0;
}
.page ul li span
{
display:block;
width:28px;
height:23px;
background-color:#2d8da3;
}
.page ul li span a
{
color:#ffffff;
}
.page ul li a
{
display:block;
width:28px;
height:19px;
text-decoration:none;
color:#354c7e;
font-size:12px;
vertical-align:middle;
padding-top:4px;
}
.page ul li a:hover
{
background-color:#2d8da3;
display:block;
width:28px;
height:19px;
}
.page .point
{
background-color:#ffffff;
width:20px;
height:24px;
display:block;
border-style:solid;
border-width:0px;
border-color:#ffffff;
}
.page .pre
{
width:48px;
height:22px;
display:block;
text-align:center;
border-style:solid;
border-width:1px;
border-color:#b1add0;
}
.page .pre a:hover
{
background-color:#2d8da3;
display:block;
width:48px;
height:19px;
}
.page .next
{
width:48px;
height:22px;
display:block;
text-align:center;
border-style:solid;
border-width:1px;
border-color:#b1add0;
}
.page .next a:hover
{
background-color:#2d8da3;
display:block;
width:48px;
height:19px;
}
/*分頁部分 end*/
/*內容部分 start*/
.content
{
width:700px;
height:120px;
border-style:solid;
border-width:1px;
border-color:#333333;
margin-bottom:10px;
}
.content_left
{
width:98px;
height:118px;
border-style:solid;
border-width:1px;
border-color:#333333;
float:left;
font-size:12px;
}
.content_right
{
margin-left:10px;
width:588px;
height:118px;
border-style:solid;
border-width:1px;
border-color:#0000ff;
float:left;
}
/*內容部分 end*/
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbl1" runat="server"></asp:Label><!--放內容-->
<asp:Label ID="lbl2" runat="server"></asp:Label><!--放頁碼-->
</div>
</form>
</body>
</html>
css沒有用.css文件寫是為了方便,呵呵……
二、再看下後台代碼(因為我是用三層寫的,所以我會把BLL層、DAL層以及Models層的代碼也附上)
web(顯示)後台.cs代碼:
復制代碼 代碼如下:
public static int pageCount = 0;//總數可以用Session保存,避免每次都要查詢,影響速度
public int pageindex;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//page頁面索引,其他參數不變,默認從第一頁開始
JBLL.JManage manage = new JBLL.JManage();
pageCount = Convert.ToInt32(manage.GetCount().Tables[0].Rows[0]["num"].ToString());
if (Request.QueryString["page"] == null)
{
pageindex=1;
}
else
{
pageindex = Convert.ToInt32(Request.QueryString["page"].ToString());
}
//加載信息和頁面
GetInfo(pageindex, 10);
Paging(pageindex, 10);
}
}
/// <summary>
/// 加載數據
/// </summary>
private void GetInfo(int pageindex, int size)
{
string strall = "";
int count;
List<JModels.JPerson> list = new JBLL.JManage().GetAllPerson(size, pageindex, "", out count);
StringBuilder sb = new StringBuilder();
foreach (JModels.JPerson p in list)
{
sb.Append("<div class='content'><div class='content_left'>");
sb.Append(p.Name);
sb.Append("</div><div class='content_right'>");
sb.Append(p.Id.ToString());
sb.Append("</div></div>");
}
strall = sb.ToString();
lbl1.Text = strall;
}
private void Paging(int pageindex, int size)
{
string strpage = "";
StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"page\"><ul>");
//需要判斷是否啟用
if (pageindex == 1)
{
sb.Append("<li class=\"pre\" style=\"display:none\"><a href=\"Page.aspx?page=" + (pageindex - 1) + "\"><<Prev</a></li>");
}
else
{
sb.Append("<li class=\"pre\"><a href=\"Page.aspx?page=" + (pageindex - 1) + "\"><<Prev</a></li>");
}
//頁碼數
int pageNum;
if (pageCount % size == 0)
{
pageNum = pageCount / size;
}
else
{
pageNum = pageCount / size + 1;
}
if (pageNum < 11)
{
for (int i = 1; i <= pageNum; i++)
{
if (i == pageindex)
{
sb.Append("<li><span><a href=\"Page.aspx?page=" + i + "\">" + i + "</a></span></li>");
}
else
{
sb.Append("<li><a href=\"Page.aspx?page=" + i + "\">" + i + "</a></li>");
}
}
}
else
{
//判斷是否是前7頁
if (pageindex > 0 && pageindex < 8)
{
for (int i = 1; i < 9; i++)
{
if (i == pageindex)
{
sb.Append("<li><span><a href=\"Page.aspx?page=" + i + "\">" + i + "</a></span></li>");
}
else
{
sb.Append("<li><a href=\"Page.aspx?page=" + i + "\">" + i + "</a></li>");
}
}
sb.Append("<li class=\"point\">...</li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageNum - 1) + "\">" + (pageNum - 1) + "</a></li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageNum) + "\">" + (pageNum) + "</a></li>");
}
else if (pageindex > 7 && pageindex < (pageNum - 6))
{
sb.Append("<li><a href=\"Page.aspx?page=1\">1</a></li>");
sb.Append("<li><a href=\"Page.aspx?page=2\">2</a></li>");
sb.Append("<li class=\"point\">...</li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageindex - 3) + "\">" + (pageindex - 3) + "</a></li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageindex - 2) + "\">" + (pageindex - 2) + "</a></li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageindex - 1) + "\">" + (pageindex - 1) + "</a></li>");
sb.Append("<li><span><a href=\"Page.aspx?page=" + pageindex + "\">" + pageindex + "</a></span></li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageindex + 1) + "\">" + (pageindex + 1) + "</a></li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageindex + 2) + "\">" + (pageindex + 2) + "</a></li>");
sb.Append("<li class=\"point\">...</li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageNum - 1) + "\">" + (pageNum - 1) + "</a></li>");
sb.Append("<li><a href=\"Page.aspx?page=" + (pageNum) + "\">" + (pageNum) + "</a></li>");
}
else
{
sb.Append("<li><a href=\"Page.aspx?page=1\">1</a></li>");
sb.Append("<li><a href=\"Page.aspx?page=2\">2</a></li>");
sb.Append("<li class=\"point\">...</li>");
for (int i = (pageNum - 7); i <= pageNum; i++)
{
if (i == pageindex)
{
sb.Append("<li><span><a href=\"Page.aspx?page=" + i + "\">" + i + "</a></span></li>");
}
else
{
sb.Append("<li><a href=\"Page.aspx?page=" + i + "\">" + i + "</a></li>");
}
}
}
}
//需要判斷是否啟用
if (pageindex == pageNum)
{
sb.Append("<li class=\"next\" style=\"display:none;\"><a href=\"Page.aspx?page=" + (pageindex + 1) + "\">Next>></a></li>");
}
else
{
sb.Append("<li class=\"next\"><a href=\"Page.aspx?page=" + (pageindex + 1) + "\">Next>></a></li>");
}
sb.Append("</ul></div>");
strpage = sb.ToString();
lbl2.Text = strpage;
}
BLL層(邏輯)代碼:
復制代碼 代碼如下:
public class JManage
{
public DataSet GetCount()
{
return JDAL.JService.GetCount();
}
///<summary>
/// 記錄總條數
///</summary>
///<returns></returns>
public DataSet GetCount(string where)
{
return JDAL.JService.GetCount(where);
}
///<summary>
/// 得到所有人
///</summary>
///<returns></returns>
public List<JModels.JPerson> GetAllPerson(int size, int index, string where, out int OutTotalCount)
{
return JDAL.JService.GetAllPerson(size, index, where, out OutTotalCount);
}
}
DAL層(數據處理)代碼:
復制代碼 代碼如下:
public static DataSet GetCount()
{
string sql = "select count(*) as num from qzDatas";
return DBHelp.GetDataSetBySql(sql);
}
///<summary>
/// 記錄總條數
///</summary>
///<returns></returns>
public static DataSet GetCount(string where)
{
string sql = "select count(*) as num from qzDatas where " + where + "";
return DBHelp.GetDataSetBySql(sql);
}
///<summary>
/// 得到所有人
///</summary>
///<returns></returns>
public static List<JModels.JPerson> GetAllPerson(int size, int index, string where, out int OutTotalCount)
{
JModels.JPerson person = null;
List<JModels.JPerson> list = new List<JModels.JPerson>();
DataTable dt = new DataTable();
string[] paramValue =
{
"qzDatas", //表明
"*", //返回字段
"qzDatasId", //主鍵標識列
where, //where條件
"qzDatasId asc", //排序必須跟有 asc 或 desc
"1", //排序規則 1:正序asc 2:倒序desc 3:多列排序方法
"0", //記錄總數 0:會返回總記錄
""+size, //頁面大小
""+index //當前頁
};
dt = DBHelp.SqlGetDataTable("P_AspNetPage", CommandType.StoredProcedure, paramValue, out OutTotalCount);
foreach (DataRow dr in dt.Rows)
{
person = new JModels.JPerson();
person.Id = Convert.ToInt32(dr["qzDatasId"]);
person.Name = dr["dataName"].ToString();
person.Imgurl = dr["url"].ToString();
person.Email = dr["dataName"].ToString();
list.Add(person);
}
return list;
}
Models層代碼:
復制代碼 代碼如下:
public class JPerson
{
//id
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
//名字
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
//頭像
private string imgurl;
public string Imgurl
{
get { return imgurl; }
set { imgurl = value; }
}
//Email
private string email;
public string Email
{
get { return email; }
set { email = value; }
}
}
以上代碼我都有注釋了,就不多解釋了,但我相信應該還有改進的地方,尤其是web顯示層的後台,添加頁碼那部分,肯定還有好辦法,如果誰有好辦法,希望能告訴我啊,大家一起學習嘛,謝謝。
三、看一下存儲過程到底是怎麼寫的
復制代碼 代碼如下:
CREATE PROCEDURE [dbo].[P_AspNetPage]
/*
nzperfect [no_mIss] 高效通用分頁存儲過程(雙向檢索) 2007.5.7 QQ:34813284
敬告:適用於單一主鍵或存在唯一值列的表或視圖
ps:Sql語句為8000字節,調用時請注意傳入參數及sql總長度不要超過指定范圍
*/
@TableName VARCHAR(200), --表名
@FieldList VARCHAR(2000), --顯示列名,如果是全部字段則為*
@PrimaryKey VARCHAR(100), --單一主鍵或唯一值鍵
@Where VARCHAR(2000), --查詢條件 不含'where'字符,如id>10 and len(userid)>9
@Order VARCHAR(1000), --排序 不含'order by'字符,如id asc,userid desc,必須指定asc或desc
--注意當@SortType=3時生效,記住一定要在最後加上主鍵,否則會讓你比較郁悶
@SortType INT, --排序規則 1:正序asc 2:倒序desc 3:多列排序方法
@RecorderCount INT, --記錄總數 0:會返回總記錄
@PageSize INT, --每頁輸出的記錄數
@PageIndex INT, --當前頁數
@TotalCount INT OUTPUT, --記返回總記錄
@TotalPageCount INT OUTPUT --返回總頁數
AS
SET NOCOUNT ON
IF ISNULL(@TotalCount,'') = '' SET @TotalCount = 0
SET @Order = RTRIM(LTRIM(@Order))
SET @PrimaryKey = RTRIM(LTRIM(@PrimaryKey))
SET @FieldList = REPLACE(RTRIM(LTRIM(@FieldList)),'','')
WHILE CHARINDEX(', ',@Order) > 0 OR CHARINDEX(' ,',@Order) > 0
BEGIN
SET @Order = REPLACE(@Order,', ',',')
SET @Order = REPLACE(@Order,' ,',',')
END
IF ISNULL(@TableName,'') = '' OR ISNULL(@FieldList,'') = ''
OR ISNULL(@PrimaryKey,'') = ''
OR @SortType < 1 OR @SortType >3
OR @RecorderCount < 0 OR @PageSize < 0 OR @PageIndex < 0
BEGIN
PRINT('ERR_00')
RETURN
END
IF @SortType = 3
BEGIN
IF (UPPER(RIGHT(@Order,4))!=' ASC' AND UPPER(RIGHT(@Order,5))!=' DESC')
BEGIN PRINT('ERR_02') RETURN END
END
DECLARE @new_where1 VARCHAR(1000)
DECLARE @new_where2 VARCHAR(1000)
DECLARE @new_order1 VARCHAR(1000)
DECLARE @new_order2 VARCHAR(1000)
DECLARE @new_order3 VARCHAR(1000)
DECLARE @Sql VARCHAR(8000)
DECLARE @SqlCount NVARCHAR(4000)
IF ISNULL(@where,'') = ''
BEGIN
SET @new_where1 = ''
SET @new_where2 = ' WHERE '
END
ELSE
BEGIN
SET @new_where1 = ' WHERE ' + @where
SET @new_where2 = ' WHERE ' + @where + ' AND '
END
IF ISNULL(@order,'') = '' OR @SortType = 1 OR @SortType = 2
BEGIN
IF @SortType = 1
BEGIN
SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' ASC'
SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' DESC'
END
IF @SortType = 2
BEGIN
SET @new_order1 = ' ORDER BY ' + @PrimaryKey + ' DESC'
SET @new_order2 = ' ORDER BY ' + @PrimaryKey + ' ASC'
END
END
ELSE
BEGIN
SET @new_order1 = ' ORDER BY ' + @Order
END
IF @SortType = 3 AND CHARINDEX(','+@PrimaryKey+'',','+@Order)>0
BEGIN
SET @new_order1 = ' ORDER BY ' + @Order
SET @new_order2 = @Order + ','
SET @new_order2 = REPLACE(REPLACE(@new_order2,'ASC,','{ASC},'),'DESC,','{DESC},')
SET @new_order2 = REPLACE(REPLACE(@new_order2,'{ASC},','DESC,'),'{DESC},','ASC,')
SET @new_order2 = ' ORDER BY ' + SUBSTRING(@new_order2,1,LEN(@new_order2)-1)
IF @FieldList <> '*'
BEGIN
SET @new_order3 = REPLACE(REPLACE(@Order + ',','ASC,',','),'DESC,',',')
SET @FieldList = ',' + @FieldList
WHILE CHARINDEX(',',@new_order3)>0
BEGIN
IF CHARINDEX(SUBSTRING(','+@new_order3,1,CHARINDEX(',',@new_order3)),','+@FieldList+',')>0
BEGIN
SET @FieldList =
@FieldList + ',' + SUBSTRING(@new_order3,1,CHARINDEX(',',@new_order3))
END
SET @new_order3 =
SUBSTRING(@new_order3,CHARINDEX(',',@new_order3)+1,LEN(@new_order3))
END
SET @FieldList = SUBSTRING(@FieldList,2,LEN(@FieldList))
END
END
SET @SqlCount = 'SELECT @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/'
+ CAST(@PageSize AS VARCHAR)+') FROM ' + @TableName + @new_where1
IF @RecorderCount = 0
BEGIN
EXEC SP_EXECUTESQL @SqlCount,N'@TotalCount INT OUTPUT,@TotalPageCount INT OUTPUT',
@TotalCount OUTPUT,@TotalPageCount OUTPUT
END
ELSE
BEGIN
SELECT @TotalCount = @RecorderCount
END
IF @PageIndex > CEILING((@TotalCount+0.0)/@PageSize)
BEGIN
SET @PageIndex = CEILING((@TotalCount+0.0)/@PageSize)
END
IF @PageIndex = 1 OR @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize)
BEGIN
IF @PageIndex = 1 --返回第一頁數據
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM '
+ @TableName + @new_where1 + @new_order1
END
IF @PageIndex >= CEILING((@TotalCount+0.0)/@PageSize) --返回最後一頁數據
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM ('
+ 'SELECT TOP ' + STR(ABS(@PageSize*@PageIndex-@TotalCount-@PageSize))
+ '' + @FieldList + ' FROM '
+ @TableName + @new_where1 + @new_order2 + ' ) AS TMP '
+ @new_order1
END
END
ELSE
BEGIN
IF @SortType = 1 --僅主鍵正序排序
BEGIN
IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向檢索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' > '
+ '(SELECT MAX(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + '' + @PrimaryKey
+ ' FROM ' + @TableName
+ @new_where1 + @new_order1 +' ) AS TMP) '+ @new_order1
END
ELSE --反向檢索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM ('
+ 'SELECT TOP ' + STR(@PageSize) + ''
+ @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' < '
+ '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@TotalCount-@PageSize*@PageIndex) + '' + @PrimaryKey
+ ' FROM ' + @TableName
+ @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
+ ' ) AS TMP ' + @new_order1
END
END
IF @SortType = 2 --僅主鍵反序排序
BEGIN
IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向檢索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' < '
+ '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + '' + @PrimaryKey
+' FROM '+ @TableName
+ @new_where1 + @new_order1 + ') AS TMP) '+ @new_order1
END
ELSE --反向檢索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM ('
+ 'SELECT TOP ' + STR(@PageSize) + ''
+ @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' > '
+ '(SELECT MAX(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@TotalCount-@PageSize*@PageIndex) + '' + @PrimaryKey
+ ' FROM ' + @TableName
+ @new_where1 + @new_order2 +' ) AS TMP) '+ @new_order2
+ ' ) AS TMP ' + @new_order1
END
END
IF @SortType = 3 --多列排序,必須包含主鍵,且放置最後,否則不處理
BEGIN
IF CHARINDEX(',' + @PrimaryKey + '',',' + @Order) = 0
BEGIN PRINT('ERR_02') RETURN END
IF @PageIndex <= CEILING((@TotalCount+0.0)/@PageSize)/2 --正向檢索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM ( '
+ 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM ( '
+ ' SELECT TOP ' + STR(@PageSize*@PageIndex) + '' + @FieldList
+ ' FROM ' + @TableName + @new_where1 + @new_order1 + ' ) AS TMP '
+ @new_order2 + ' ) AS TMP ' + @new_order1
END
ELSE --反向檢索
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM ( '
+ 'SELECT TOP ' + STR(@PageSize) + '' + @FieldList + ' FROM ( '
+ ' SELECT TOP ' + STR(@TotalCount-@PageSize *@PageIndex+@PageSize) + '' + @FieldList
+ ' FROM ' + @TableName + @new_where1 + @new_order2 + ' ) AS TMP '
+ @new_order1 + ' ) AS TMP ' + @new_order1
END
END
END
EXEC(@Sql)
存儲過程應該寫得很詳細了...我就不解釋了。這個存儲過程的用處很多啊,如果想實現無刷新的分頁的話也是可以用的,調用的方法基本一樣,大家可以自己試試。
好吧,到這裡代碼也基本上完成了,我認為主要的還是思路問題,思路有了就好辦了,代碼如果哪裡有誤或者有哪些更好的解決方案,請告訴我啊,一定虛心學習哈。下面看下效果圖吧!效果圖如下:
有一個注意點哦:進入頁面的時候一定要給參數page一個值,不然的話,會出錯,你懂的呀!如:http://localhost:60284/Page.aspx?page=88