asp.net+Ajax 文本文件靜態(tài)分頁(yè)實(shí)現(xiàn)代碼
2024-09-01 08:29:17
供稿:網(wǎng)友
服務(wù)端部分 ,文本文件分頁(yè)的類。主要在流中處理。當(dāng)然我看過(guò)網(wǎng)上的用</br> 關(guān)鍵字進(jìn)行分頁(yè)的
個(gè)人覺(jué)得不是所有時(shí)候都能滿足要求,所一自己寫(xiě)了這個(gè),還是費(fèi)了點(diǎn)時(shí)間,主要在于本人太笨,基礎(chǔ)很差。希望大家個(gè)出更好的建議
代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Txt
{
public class TxtPager
{
public TxtPager()
{ }
public TxtPager(string _txtPath, int _Pagesize)
{
{
this.txtPath = _txtPath;
this.pageSize = _Pagesize;
}
}
string txtPath;//文件路徑
int pageSize;//每頁(yè)文本行數(shù)
// int ppt;//
int number;
// int totalPage;
#region
public int TotalPage
{
get {
if (TxtLineCount() % pageSize == 0)
return TxtLineCount() / pageSize;
else
return TxtLineCount()/pageSize + 1; }
}
public int Ppt
{
get { return TxtLineCount(); }
}
public int Number
{
get { return number; }
set { number = value; }
}
#endregion
private int TxtLineCount()
{
StreamReader sr = new StreamReader(this.txtPath);
string line;
int count = 0;
while ((line = sr.ReadLine()) != null)
{
//line += "fuck";
count++;
}
return count;
}
public string ReadTxtToHtml()
{
string line;//存放一行文字
int ptr = 0;//行計(jì)數(shù)
int ttp = 1;//分頁(yè)后的最大頁(yè)數(shù)
StreamReader sr = new StreamReader(txtPath);
string htmlStr = "";//用于存放Html代碼
htmlStr += "#" + ttp + "</br>";
while ((line = sr.ReadLine()) != null)
{
if (ptr == pageSize)
{
ttp++;
htmlStr += "#" + ttp + "</br>";
ttp++;
htmlStr += "#" + ttp + "</br>";
ptr = 0;
}
htmlStr += line + "</br>";
ptr++;
}
htmlStr += "#" + (ttp + 1) ;
//return htmlStr;
if (number > ttp+1/2)
{
number = ttp;
}
//.................................
string startStr = "#" + (2 * number - 1);//1
string endStr = "#" + (2 * number);//2 1---2
int startNum = htmlStr.IndexOf(startStr);
int endNum = htmlStr.IndexOf(endStr);
int offset = startStr.Length;
return htmlStr.Substring(startNum + offset, endNum - (startNum + offset));
}
}
}
這里是這個(gè)類的使用方法:
這段代碼用來(lái)解釋分頁(yè)類的使用有一點(diǎn)不直觀,主要是寫(xiě)的時(shí)候我是針對(duì)多的文件分頁(yè)的,還好我這里只需要6個(gè)而已;需要多個(gè)也可也滿足要求。
呵呵還沒(méi)有完善,注釋部分懶得寫(xiě),所以沒(méi)寫(xiě),哎是在是太懶了。注冊(cè)這么長(zhǎng)時(shí)間的博客園才寫(xiě)這么幾篇爛東西。跟自己的初衷還是想去甚遠(yuǎn)的。
代碼如下:
public partial class TxtPager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int pageSize = 40;
string _path = rtPath(int.Parse(Request.QueryString["txtid"]));