在网站后台编辑长篇文章时加入 什么简单代码可实现文章分页显示
之前看朋友在网上后台添加了一篇很长的文章,中间加了个代码实现了长文章分页显示的效果,好像记得有p有#号。记不准了?是很简单的代码,谁知道请帮助一下。...
之前看朋友在网上后台添加了一篇很长的文章,中间加了个代码实现了长文章分页显示的效果,好像记得有p有#号。记不准了?是很简单的代码,谁知道请帮助一下。
展开
2个回答
2013-07-19
展开全部
在button的事件里写上自己的代码,当然就是直接insert innerhtml自己的分页标志了,我目前用的是|,|,|,|,|,|当然可以自定义其他的。首先提供个类库。
1 ContentPager .cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
/// <summary>
/// ContentPager 的摘要说明
/// </summary>
public class ContentPager : System.Web.UI.Page
{
private System.Web.UI.WebControls.HyperLink lnknext;
private System.Web.UI.WebControls.HyperLink lnkprev;
private System.Web.UI.WebControls.HyperLink lnkfist;
private System.Web.UI.WebControls.HyperLink lnklast;
private System.Web.UI.WebControls.Label pagenum;
private System.Web.UI.WebControls.Label pagecount;
private System.Web.UI.WebControls.Label displaycontent;
public ContentPager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void datapager(string content,string query)
{
string PagerSplit = System.Configuration.ConfigurationManager.AppSettings["PagerSplit"];//从web.config中获取分页符,当然这里可以自己定义,为了方便还是放到配置文件或者数据库中。
string[] contentArray = StringSplit(content, PagerSplit);//这里是一个方法,摘自网上
int PageCount = contentArray.Length;
this.pagecount.Text = "共<b><font color='#FF0000'>" + PageCount.ToString() + "</font></b>页";
int CurPage;
//确定当前页数
if (System.Web.HttpContext.Current.Request.Params["Page"] != null)
{
CurPage = Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]);
}
else
{
CurPage = 1;
}
if (CurPage < 1) CurPage = 1;
if (Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]) > PageCount)
{
CurPage = PageCount;
}
//显示当前页数
this.pagenum.Text = "第<b><font color='#FF0000'>" + CurPage.ToString() + "</font></b>页";
//下一页
if (CurPage != PageCount)
{
lnknext.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1) + query;
}
//上一页
if (CurPage > 1)
{
lnkprev.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1) + query;
}
//第一页
if (CurPage != 1)
{
lnkfist.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1) + query;
}
//最后一页
if (CurPage != PageCount)
{
lnklast.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(PageCount) + query;
}
displaycontent.Text = contentArray[CurPage - 1].ToString();
}
#region System.Web.UI.WebControls.HyperLink DisplayContent
public System.Web.UI.WebControls.Label DisplayContent
{
get
{
return this.DisplayContent;
}
set
{
this.displaycontent = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnkprev
public System.Web.UI.WebControls.HyperLink Lnkprev
{
get
{
return this.lnkprev;
}
set
{
this.lnkprev = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnknext
public System.Web.UI.WebControls.HyperLink Lnknext
{
get
{
return this.lnknext;
}
set
{
this.lnknext = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnkfist
public System.Web.UI.WebControls.HyperLink Lnkfist
{
get
{
return this.lnkfist;
}
set
{
this.lnkfist = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnklast
public System.Web.UI.WebControls.HyperLink Lnklast
{
get
{
return this.lnklast;
}
set
{
this.lnklast = value;
}
}
#endregion
#region System.Web.UI.WebControls.Label Pagenum
public System.Web.UI.WebControls.Label Pagenum
{
get
{
return this.pagenum;
}
set
{
this.pagenum = value;
}
}
#endregion
#region System.Web.UI.WebControls.Label Pagecount
public System.Web.UI.WebControls.Label Pagecount
{
get
{
return this.pagecount;
}
set
{
this.pagecount = value;
}
}
#endregion
#region 将字符串分割成数组
/// <summary>
/// 将字符串分割成数组
/// </summary>
/// <param name="strSource"></param>
/// <param name="strSplit"></param>
/// <returns></returns>
public string[] StringSplit(string strSource, string strSplit)
{
string[] strtmp = new string[1];
int index = strSource.IndexOf(strSplit, 0);
if (index < 0)
{
strtmp[0] = strSource;
return strtmp;
}
else
{
strtmp[0] = strSource.Substring(0, index);
return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
}
}
#endregion
#region 采用递归将字符串分割成数组
/// <summary>
/// 采用递归将字符串分割成数组
/// </summary>
/// <param name="strSource"></param>
/// <param name="strSplit"></param>
/// <param name="attachArray"></param>
/// <returns></returns>
private string[] StringSplit(string strSource, string strSplit, string[] attachArray)
{
string[] strtmp = new string[attachArray.Length + 1];
attachArray.CopyTo(strtmp, 0);
int index = strSource.IndexOf(strSplit, 0);
if (index < 0)
{
strtmp[attachArray.Length] = strSource;
return strtmp;
}
else
{
strtmp[attachArray.Length] = strSource.Substring(0, index);
return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
}
}
#endregion
}
2 调用部分:
在aspx中增加lable等元素。
<asp:label id="currentpage" runat="server" CssClass="gray">当前页</asp:label>
<asp:label id="totalpage" runat="server" CssClass="gray">Label</asp:label>
<asp:hyperlink id="lnkfist" runat="server" CssClass="gray">首页</asp:hyperlink>|
<asp:hyperlink id="lnkprev" runat="server" CssClass="gray">前页</asp:hyperlink>|
<asp:hyperlink id="lnknext" runat="server" CssClass="gray">下页</asp:hyperlink>|
<asp:hyperlink id="lnklast" runat="server" CssClass="gray">尾页</asp:hyperlink>
在aspx.cs中增加调用代码。
private ContentPager pager;
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"]);//接收参数,例如访问这个页是xxx.aspx?id=1
DataSet ds = new DataSet();
//从数据库读内容部分就略掉了
DataRow drow;
drow = ds.Tables[0].Rows[0];
string cont = drow["content"].ToString();//此部分可以把内容中的HTML代码转换一下,我也省略了
//文章分页,关键部分
pager = new ContentPager();
pager.Lnknext = this.lnknext;
pager.Lnkprev = this.lnkprev;
pager.Lnklast = this.lnklast;
pager.Lnkfist = this.lnkfist;
pager.Pagenum = this.currentpage;
pager.Pagecount = this.totalpage;
pager.DisplayContent = this.content;
string query = "&id=" + Request.QueryString["id"].ToString();
pager.datapager(cont, query);
}
1 ContentPager .cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
/// <summary>
/// ContentPager 的摘要说明
/// </summary>
public class ContentPager : System.Web.UI.Page
{
private System.Web.UI.WebControls.HyperLink lnknext;
private System.Web.UI.WebControls.HyperLink lnkprev;
private System.Web.UI.WebControls.HyperLink lnkfist;
private System.Web.UI.WebControls.HyperLink lnklast;
private System.Web.UI.WebControls.Label pagenum;
private System.Web.UI.WebControls.Label pagecount;
private System.Web.UI.WebControls.Label displaycontent;
public ContentPager()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void datapager(string content,string query)
{
string PagerSplit = System.Configuration.ConfigurationManager.AppSettings["PagerSplit"];//从web.config中获取分页符,当然这里可以自己定义,为了方便还是放到配置文件或者数据库中。
string[] contentArray = StringSplit(content, PagerSplit);//这里是一个方法,摘自网上
int PageCount = contentArray.Length;
this.pagecount.Text = "共<b><font color='#FF0000'>" + PageCount.ToString() + "</font></b>页";
int CurPage;
//确定当前页数
if (System.Web.HttpContext.Current.Request.Params["Page"] != null)
{
CurPage = Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]);
}
else
{
CurPage = 1;
}
if (CurPage < 1) CurPage = 1;
if (Convert.ToInt32(System.Web.HttpContext.Current.Request.Params["Page"]) > PageCount)
{
CurPage = PageCount;
}
//显示当前页数
this.pagenum.Text = "第<b><font color='#FF0000'>" + CurPage.ToString() + "</font></b>页";
//下一页
if (CurPage != PageCount)
{
lnknext.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1) + query;
}
//上一页
if (CurPage > 1)
{
lnkprev.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1) + query;
}
//第一页
if (CurPage != 1)
{
lnkfist.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1) + query;
}
//最后一页
if (CurPage != PageCount)
{
lnklast.NavigateUrl = System.Web.HttpContext.Current.Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(PageCount) + query;
}
displaycontent.Text = contentArray[CurPage - 1].ToString();
}
#region System.Web.UI.WebControls.HyperLink DisplayContent
public System.Web.UI.WebControls.Label DisplayContent
{
get
{
return this.DisplayContent;
}
set
{
this.displaycontent = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnkprev
public System.Web.UI.WebControls.HyperLink Lnkprev
{
get
{
return this.lnkprev;
}
set
{
this.lnkprev = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnknext
public System.Web.UI.WebControls.HyperLink Lnknext
{
get
{
return this.lnknext;
}
set
{
this.lnknext = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnkfist
public System.Web.UI.WebControls.HyperLink Lnkfist
{
get
{
return this.lnkfist;
}
set
{
this.lnkfist = value;
}
}
#endregion
#region System.Web.UI.WebControls.HyperLink Lnklast
public System.Web.UI.WebControls.HyperLink Lnklast
{
get
{
return this.lnklast;
}
set
{
this.lnklast = value;
}
}
#endregion
#region System.Web.UI.WebControls.Label Pagenum
public System.Web.UI.WebControls.Label Pagenum
{
get
{
return this.pagenum;
}
set
{
this.pagenum = value;
}
}
#endregion
#region System.Web.UI.WebControls.Label Pagecount
public System.Web.UI.WebControls.Label Pagecount
{
get
{
return this.pagecount;
}
set
{
this.pagecount = value;
}
}
#endregion
#region 将字符串分割成数组
/// <summary>
/// 将字符串分割成数组
/// </summary>
/// <param name="strSource"></param>
/// <param name="strSplit"></param>
/// <returns></returns>
public string[] StringSplit(string strSource, string strSplit)
{
string[] strtmp = new string[1];
int index = strSource.IndexOf(strSplit, 0);
if (index < 0)
{
strtmp[0] = strSource;
return strtmp;
}
else
{
strtmp[0] = strSource.Substring(0, index);
return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
}
}
#endregion
#region 采用递归将字符串分割成数组
/// <summary>
/// 采用递归将字符串分割成数组
/// </summary>
/// <param name="strSource"></param>
/// <param name="strSplit"></param>
/// <param name="attachArray"></param>
/// <returns></returns>
private string[] StringSplit(string strSource, string strSplit, string[] attachArray)
{
string[] strtmp = new string[attachArray.Length + 1];
attachArray.CopyTo(strtmp, 0);
int index = strSource.IndexOf(strSplit, 0);
if (index < 0)
{
strtmp[attachArray.Length] = strSource;
return strtmp;
}
else
{
strtmp[attachArray.Length] = strSource.Substring(0, index);
return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
}
}
#endregion
}
2 调用部分:
在aspx中增加lable等元素。
<asp:label id="currentpage" runat="server" CssClass="gray">当前页</asp:label>
<asp:label id="totalpage" runat="server" CssClass="gray">Label</asp:label>
<asp:hyperlink id="lnkfist" runat="server" CssClass="gray">首页</asp:hyperlink>|
<asp:hyperlink id="lnkprev" runat="server" CssClass="gray">前页</asp:hyperlink>|
<asp:hyperlink id="lnknext" runat="server" CssClass="gray">下页</asp:hyperlink>|
<asp:hyperlink id="lnklast" runat="server" CssClass="gray">尾页</asp:hyperlink>
在aspx.cs中增加调用代码。
private ContentPager pager;
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"]);//接收参数,例如访问这个页是xxx.aspx?id=1
DataSet ds = new DataSet();
//从数据库读内容部分就略掉了
DataRow drow;
drow = ds.Tables[0].Rows[0];
string cont = drow["content"].ToString();//此部分可以把内容中的HTML代码转换一下,我也省略了
//文章分页,关键部分
pager = new ContentPager();
pager.Lnknext = this.lnknext;
pager.Lnkprev = this.lnkprev;
pager.Lnklast = this.lnklast;
pager.Lnkfist = this.lnkfist;
pager.Pagenum = this.currentpage;
pager.Pagecount = this.totalpage;
pager.DisplayContent = this.content;
string query = "&id=" + Request.QueryString["id"].ToString();
pager.datapager(cont, query);
}
2013-07-19
展开全部
你所说的,那个是dede织梦的分页代码。在别的系统程序上是用不了的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询