asp.net+Ajax 文本文件靜態分頁實現代碼
2024-09-01 08:29:17
供稿:網友
服務端部分 ,文本文件分頁的類。主要在流中處理。當然我看過網上的用</br> 關鍵字進行分頁的
個人覺得不是所有時候都能滿足要求,所一自己寫了這個,還是費了點時間,主要在于本人太笨,基礎很差。希望大家個出更好的建議
代碼如下:
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;//每頁文本行數
// 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;//行計數
int ttp = 1;//分頁后的最大頁數
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));
}
}
}
這里是這個類的使用方法:
這段代碼用來解釋分頁類的使用有一點不直觀,主要是寫的時候我是針對多的文件分頁的,還好我這里只需要6個而已;需要多個也可也滿足要求。
呵呵還沒有完善,注釋部分懶得寫,所以沒寫,哎是在是太懶了。注冊這么長時間的博客園才寫這么幾篇爛東西。跟自己的初衷還是想去甚遠的。
代碼如下:
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"]));