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

asp.net 长文章通过设定的行数分页

程序员文章站 2024-03-08 15:24:04
复制代码 代码如下:public string outputbyline(string strcontent)//通过设定的行数分页 { int pagesize = in...
复制代码 代码如下:

public string outputbyline(string strcontent)//通过设定的行数分页
{
int pagesize = int.parse(configurationmanager.appsettings["pagesize"]);//每页显示行数从config文件中取出
string linebreak = configurationmanager.appsettings["linebreak"];//换行符从config文件中取出
string linebreaks = "<" + linebreak + ">";
string linebreake = "</" +linebreak+">";
strcontent = strcontent.replace("\r\n", "");
string[] strlined = strcontent.split(new string[] {linebreaks, linebreake }, stringsplitoptions.removeemptyentries);//以div为换行符
int pagecount = strlined.length / pagesize;
int pagecountplus = strlined.length % pagesize == 0 ? 0 : 1;//非满页
pagecount = pagecount + pagecountplus;//总页数
int currentpage = 1;//当前页码
string displaytext = null;
if (request.querystring["pageindex"]!=null) //获取翻页页码
{
currentpage = convert.toint32(request.querystring["pageindex"].tostring());
}
string pageinfo = "";//页数信息
for (int i = 1; i < pagecount+1; i++)
{

if (i==currentpage)
{
pageinfo += " 第" + i + "页";
if (pagecount>1)
{
pageinfo += " | ";
}
}
else
{
pageinfo += string.format("<a href='newshow.aspx?pageindex={0}' title='翻到第{0}页'>{0} | </a>",i);
}
}
labpagenumber.text = pageinfo;
for (int i = (currentpage-1)*pagesize; i < currentpage*pagesize&&i<strlined.length; i++)
{
displaytext += "<div>" + strlined[i] + "</div>";
}
return displaytext;
}