1.此方法为核心方法

代码
/// <summary>
///文章分页函数
/// </summary>
/// <param name="content">文章内容</param>
/// <param name="currentPage">当前页码</param>
/// <param name="pageUrl">当前页面地址</param>
protected String[] ArticlePage(string content, int currentPage, string pageUrl)
{
String[] result
= new String[2];
int pageCount = 0;//页数
content = content.Replace("<div style=\"page-break-after: always\"><span style=\"display: none\">&nbsp;</span></div>", "[--page--]");//FCK在IE中生成的默认分页符,替换为自定义分页符
string[] tempContent = System.Text.RegularExpressions.Regex.Split(content, "\\u005B--page--]"); //取得分页符 "\\u005B"为"["的转义
pageCount = tempContent.Length;
string outputContent = "";//要输出的内容

if (pageCount <= 1)
{
outputContent
= content; //文章内容
result[0] = String.Empty;

}
else
{
string pageStr = "";//分页字符串
if (currentPage != 1)
{
pageStr
+= "<a class='prev' href =" + pageUrl + "_" + (currentPage - 1) + ".html>&nbsp;</a>";
}

for (int i = 1; i <= pageCount; i++)
{
if (i == currentPage)
pageStr
+= ("<span class='active'>[" + i + "]</span>");
else
pageStr
+= ("<a class='num' href =" + pageUrl + "_" + i + ".html>[" + i + "]</a>");
}

if (currentPage != pageCount)
{
pageStr
+= "<a class='next' href =" + pageUrl + "_" + (currentPage + 1) + ".html>&nbsp;</a>";
}
result[
0] = pageStr;
outputContent
= tempContent[currentPage - 1].ToString();
}
result[
1] = outputContent;
return result;
}

 

Controller部分

代码
public ActionResult Attractions(int id,int? pageid)
{
Pic_info picinfo
= new PicDal().GetPic_infoByID(id);
String[] result
= ArticlePage(picinfo.Pic_Context, pageid ?? 1, "/Attractions/list_" + id);
picinfo.Pic_Context
= result[1];

ViewData[
"page_num"] = result[0];
ViewData[
"type"] = 1;
return View(picinfo);

}

 

View页面:

代码
<% Pic_info pic = Model as Pic_info;
if (pic != null)
{
%>
<h3><%= pic.Pic_name%></h3>
<div>
<%= pic.Pic_Context%>
</div>
<div class="page_num"><%= ViewData["page_num"].ToString()%></div>
<% } %>

 

 CSS样式:

 

代码
.page_num
{
position
:relative;
margin
:0 auto;
width
:300px;
margin-top
:10px;
}
.prev,.next
{
height
:21px;
width
:69px;
display
:block;
float
:left;
margin
:0 2px;
text-decoration
:none;
}
.prev
{
background
:url(images/page_up.gif) no-repeat;
}
.next
{
background
:url(images/page_down.gif) no-repeat;
}
.count
{
float
:left;
margin-right
:40px;
margin-top
:4px;

}
.num,.active
{
float
:left;
font-size
:14px;
text-decoration
:none;
padding
:2px 2px 0 2px;
}
.num
{
color
:#000;
}
.num:hover
{
color
:#0068BE;
}
.active
{
color
: #0068BE;
}

 

预览:

 

 

 

posted on 2010-06-18 17:14  θ⒎哖紅酒  阅读(1147)  评论(9编辑  收藏  举报