欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net利用存储过程和div+css实现分页(类似于博客园首页分页)

程序员文章站 2024-03-06 11:31:37
首先声明以下几点: 1、也许讲解有点初级,希望高手不要“喷”我,因为我知道并不是每一个人都是高手,我也怕高手们说我装13,小生不才; 2、如有什么不对的地方,还希望大家指出...
首先声明以下几点:
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)

存储过程应该写得很详细了...我就不解释了。这个存储过程的用处很多啊,如果想实现无刷新的分页的话也是可以用的,调用的方法基本一样,大家可以自己试试。
  好吧,到这里代码也基本上完成了,我认为主要的还是思路问题,思路有了就好办了,代码如果哪里有误或者有哪些更好的解决方案,请告诉我啊,一定虚心学习哈。下面看下效果图吧!效果图如下:

asp.net利用存储过程和div+css实现分页(类似于博客园首页分页)

有一个注意点哦:进入页面的时候一定要给参数page一个值,不然的话,会出错,你懂的呀!如:http://localhost:60284/Page.aspx?page=88