新聞中心
asp.net中怎么利用Ajax實現(xiàn)文本文件靜態(tài)分頁,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了龍巖免費(fèi)建站歡迎大家使用!
服務(wù)端部分 ,文本文件分頁的類。主要在流中處理。當(dāng)然我看過網(wǎng)上的用 關(guān)鍵字進(jìn)行分頁的
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;//每頁文本行數(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;//行計數(shù) int ttp = 1;//分頁后的最大頁數(shù) StreamReader sr = new StreamReader(txtPath); string htmlStr = "";//用于存放Html代碼 htmlStr += "#" + ttp + ""; while ((line = sr.ReadLine()) != null) { if (ptr == pageSize) { ttp++; htmlStr += "#" + ttp + ""; ttp++; htmlStr += "#" + ttp + ""; ptr = 0; } htmlStr += line + ""; 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"])); string path = HttpContext.Current.Server.MapPath(_path); Txt.TxtPager tp = new Txt.TxtPager(path, pageSize); int _ttpage = tp.TotalPage; if (Request.RequestType.ToString().ToLower() == "get") { Response.Write(_ttpage.ToString()); Response.End(); } if (Request.RequestType.ToLower() == "post") { //string = //前端判斷 index 是否為數(shù)字 string index = Request.QueryString["index"]; tp.Number = int.Parse(index); string context = tp.ReadTxtToHtml(); Response.Write(context); Response.End(); } } private string rtPath(int i) { string[] pathCollection = { "Test.txt" ,"tdays.txt","problem.txt","jion.txt","serve.txt","project.txt","icexplain.txt"}; return "txt/" + pathCollection[i]; } }
上面這也是為Ajax 的請求做準(zhǔn)備的,寫的還是很粗糙的,希望高手莫扔我雞蛋
Ajax 代碼部分
代碼還是很淺顯易懂的,呵呵因為我也不會寫高深的代碼。重要的我們都要動手去實踐。菜鳥們多多努力哦。做完我這個例子級會收獲很多哦。
至少當(dāng)我還是個初級菜鳥的時候要是有這個例子進(jìn)步會很快。
冰激凌制作方法大全 |
td> |
關(guān)于asp.net中怎么利用Ajax實現(xiàn)文本文件靜態(tài)分頁問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
網(wǎng)站標(biāo)題:asp.net中怎么利用Ajax實現(xiàn)文本文件靜態(tài)分頁
URL鏈接:http://fisionsoft.com.cn/article/geojii.html