62,050
社区成员
发帖
与我相关
我的任务
分享
string allHtml= @"<[^>]*>";//匹配所有Html
string partHtml= @"<(?!br|\/?p|img)[^>]*>";//部分Html不匹配
ArticlePaing articlePaging = new ArticlePaing();
LiteralHtml.Text = articlePaging.CoustomPaging("未分页的文章");
#region "Using"
using System;
using System.Text;
using System.Web;
using System.Web.Caching;
#endregion
namespace Teleware.Modules.Articles {
/// <summary>
/// 文章内容分页类
/// </summary>
public class ArticlePaing {
#region "Constructor"
/// <summary>
/// 构造函数初始一些默认值
/// </summary>
public ArticlePaing() {
ResetCache();
PageNumber = 1;
SelectPageStyle = "style='color:red'";
PageSize = 10;
}
#endregion
#region "Properties"
/// <summary>
/// 分页分隔标记
/// </summary>
public const string SpiltPageTag = "#Page#";
/// <summary>
/// 分页后连接和内容部分模板
/// </summary>
public const string PagingTemplate = @"<table width='100%' >
<tr><td style='height:300px' valign='top'>#Content#</td></tr>
<tr><td align='center'>#NavigatorUrl#</td></tr>
</table>";
/// <summary>
/// 文章内容缓存Key
/// </summary>
private const string ContentCacheKey = "ContentCacheKey";
/// <summary>
/// 页面大小|自动分页时指每页字符数
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 总记录数
/// </summary>
public int TotalCount { get; set; }
/// <summary>
/// 页号
/// </summary>
public int PageNumber { get; set; }
/// <summary>
/// 页数
/// </summary>
public int PageCount { get; set; }
/// <summary>
/// 选择页面样式
/// </summary>
public string SelectPageStyle { get; set; }
#endregion
#region "Public Method"
public string CoustomPaging(string content) {
TotalCount = GetPages(content).Length;
//计算出总页数
PageCount = TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1;
string articlePage = HttpContext.Current.Request["ArticlePage"];
string isLeave = HttpContext.Current.Request["IsLeft"];
//分页模板,主要是用于分页内容和分页链接的布局
string template = PagingTemplate;
if (!string.IsNullOrEmpty(articlePage)) {
PageNumber = int.Parse(articlePage);
}
if (PageNumber > TotalCount) {
PageNumber = TotalCount;
}
if (PageNumber != -1 && PageNumber < 1) {
PageNumber = 1;
}
//如果余下参数不为空直接返回当前页余下的所有文章内容
if (isLeave != null) {
return template.Replace("#Content#", GetLeavePage(content)).Replace("#NavigatorUrl#", "");
}
return template.Replace("#Content#", GetPages(content)[PageNumber - 1]).Replace("#NavigatorUrl#", CreateNavigatorUrl(content));
}
#endregion
#region "Private Method"
/// <summary>
/// 获取所有页面数据(并将数据缓存)
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
private string[] GetPages(string content) {
Cache cache = HttpContext.Current.Cache;
if (cache[ContentCacheKey] == null) {
string[] Pages = content.Split(new string[] { SpiltPageTag }, StringSplitOptions.RemoveEmptyEntries);
cache[ContentCacheKey] = Pages;
return Pages;
} else {
return cache[ContentCacheKey] as string[];
}
}
/// <summary>
/// 获取页面跳转的Url
/// </summary>
/// <param name="content">文章内容</param>
/// <returns></returns>
private string CreateNavigatorUrl(string content) {
StringBuilder sb = new StringBuilder();
//计算出当前文章所在页,已经取出所在组开始索引和结束索引
int start = 0, end = 0;
for (int j = 0; j < PageCount; j++) {
//判断文章所在页区间
if (PageNumber > j * PageSize && PageNumber <= (j + 1) * PageSize) {
start = j * PageSize;
//如果是最后一页,结束索引则等于总记录数
if (j == PageCount - 1) {
end = TotalCount;
} else {
end = (j + 1) * PageSize;
}
}
}
//上一页
if (PageNumber > 1) {
sb.Append(GetPreviousUrl());
}
//中间部分
for (; start < end; start++) {
//设置选中样式
if (PageNumber == start + 1) {
sb.Append(GetAppointUrl(start + 1, SelectPageStyle));
} else {
sb.Append(GetAppointUrl(start + 1, string.Empty));
}
}
if (PageNumber < TotalCount) {
//下一页
sb.Append(GetNextUrl());
//剩下所有页
sb.Append(GetLeaveUrl());
}
return sb.ToString();
}
/// <summary>
/// 生成链接Url
/// </summary>
/// <param name="arg1">参数</param>
/// <param name="strShow">链接显示的文字</param>
private string CreateLink(object url, string style, string strShow) {
return string.Format(" <a href='{0}' {1} > {2} </a>", url, style, strShow);
}
/// <summary>
/// 获取下页链接
/// </summary>
private string GetNextUrl() {
return CreateLink(string.Format("{0}&ArticlePage={1}", GetFirstRequestUrl(), PageNumber + 1), string.Empty, "下一页");
}
/// <summary>
/// 获取上页链接
/// </summary>
private string GetPreviousUrl() {
return CreateLink(string.Format("{0}&ArticlePage={1}", GetFirstRequestUrl(), PageNumber - 1), string.Empty, "上一页");
}
/// <summary>
/// 获取余下页链接
/// </summary>
private string GetLeaveUrl() {
return CreateLink(string.Format("{0}&ArticlePage={1}&IsLeft=true", GetFirstRequestUrl(), PageNumber), string.Empty, "余下页");
}
/// <summary>
/// 获取指定页链接
/// </summary>
/// <param name="PageNumber">页号</param>
/// <param name="selectStyle">链接样式</param>
private string GetAppointUrl(int PageNumber, string selectStyle) {
return CreateLink(string.Format("{0}&ArticlePage={1}", GetFirstRequestUrl(), PageNumber), selectStyle, "[" + PageNumber + "]");
}
/// <summary>
/// 获取从当前页开始剩下的所有页面
/// </summary>
/// <param name="content"></param>
/// <param name="PageNumer"></param>
/// <returns></returns>
private string GetLeavePage(string content) {
StringBuilder sb = new StringBuilder();
for (int i = PageNumber; i < GetPages(content).Length; i++) {
sb.Append(GetPages(content)[i]);
}
return sb.ToString();
}
/// <summary>
/// 如果请求地址无页分页参数,则清空缓存
/// </summary>
private void ResetCache() {
if (HttpContext.Current.Request.Url.ToString().IndexOf("&ArticlePage=") == -1) {
Cache cache = HttpContext.Current.Cache;
if (cache[ContentCacheKey] != null) {
cache.Remove(ContentCacheKey);
}
}
}
/// <summary>
/// 获取第一请求连接
/// </summary>
/// <returns></returns>
private string GetFirstRequestUrl() {
string url = HttpContext.Current.Request.Url.ToString();
if (url.IndexOf("&ArticlePage=") != -1) {
url = url.Substring(0, url.IndexOf("&ArticlePage="));
}
return url;
}
#endregion
}
}