ASP. Net + Ajax text file static paging implementation code

Time:2021-12-8

Server part, the class of text file paging. It is mainly processed in the stream. Of course, I’ve seen online pagination with < / BR > keywords
Personally, I don’t think I can meet the requirements all the time. I took some time to write this myself, mainly because I’m too stupid and have a poor foundation. I hope you can make better suggestions

Copy codeThe code is as follows:
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;// File path
int pageSize;// Lines of text per page
// 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;// Store a line of text
int ptr = 0;// Row count
int ttp = 1;// Maximum number of pages after Pagination
StreamReader sr = new StreamReader(txtPath);
string htmlStr = “”;// Used to store HTML code
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));
}
}
}

Here is how to use this class:
This code is used to explain the use of paging classes. It’s a little intuitive. It’s mainly because I’m paging multiple files when writing. Fortunately, I only need 6 here; More than one can also meet the requirements.
Oh, it’s not perfect. I’m too lazy to write the notes, so I didn’t write it. Alas, I’m too lazy. Only after registering for such a long time can I write so many rotten things. I still want to go far from my original intention.

Copy codeThe code is as follows:
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 =
//The front end judges whether the index is a number
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];
}
}

The above is also prepared for Ajax requests. The writing is still very rough. I hope experts don’t throw me eggs
Ajax code section

Copy codeThe code is as follows:
<script type=”text/javascript” language=”javascript”>
var xmlhttp;
var pxmlhttp;
var TotilPage;
var ContectBody;
function GetXmlHttpRequset()
{
var rtxmlHttp;
try
{ rtxmlHttp = new XmlHttpRequset();}
catch(e)
{
try
{
rtxmlHttp = new ActiveXObject(“Mxxml2.XMLHTTP”);
}
catch(e)
{
try{ rtxmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);}
catch(e)
{alert (“your browser does not support Ajax”);}
}
}
return rtxmlHttp;
}
function GetPagerCount()
{
xmlhttp = GetXmlHttpRequset();
var url = “TxtPager.aspx?txtid=0”;
try
{
xmlhttp.open(“get”,url,false);
}
catch(e)
{ return false; }
xmlhttp.onreadystatechange = CountCallBack;
xmlhttp.send(null);
return true;
}
function CountCallBack()
{
if(xmlhttp.readyState == 4)
{
var count = xmlhttp.responseText;
var i = 0;
for(i=0 ; i<count ; i++)
{
var ChildDiv = document.createElement(“div”);
document.getElementById(“Count1”).appendChild( ChildDiv);
ChildDiv.innerHTML = i+1;
ChildDiv.setAttribute(“id”,i);
ChildDiv.setAttribute(“onclick”,”GetHtmlString(“+(i+1)+”)”);
GetHtmlString(1);
}
}
}
function GetHtmlString(i)
{
pxmlhttp = GetXmlHttpRequset();
var Url = “TxtPager.aspx?txtid=0&index=”+ i;
pxmlhttp.open(“post”,Url,false);
pxmlhttp.onreadystatechange = ContextPostBack ;
pxmlhttp.send(null);
}
function ContextPostBack()
{
if(pxmlhttp.readyState == 4)
{
var context = pxmlhttp.responseText;
document.getElementById(“article”).innerHTML = context;
}
}
</script>

The code is still very simple and easy to understand, ha ha, because I can’t write advanced code. The important thing is that we all have to practice. Rookies work hard. I will gain a lot after finishing my example level.
At least when I was a beginner, if I had this example, I would make rapid progress.
The following is the displayed part. Give you a complete picture. Be kind and responsible

Copy codeThe code is as follows:
<table width=”90%” border=”0″ cellspacing=”0″ cellpadding=”0″>
<tr>
< TD align = “center” > complete recipe for ice cream</td>
</tr>
<tr>
<td align=”left” style=”font-size:12px; height:805px”>td>
</tr>
<tr>
<td style=”height:auto; text-align:right”> </td>
</tr>
</table>

It’s done!! I hope people who come to see my code will not be disappointed.

Recommended Today

Heavyweight Tencent cloud open source industry’s first etcd one-stop governance platform kstone

​ Kstone open source At the kubecon China Conference held by CNCF cloud native foundation on December 9, 2021,Tencent cloud container tke team released the open source project of kstone etcd governance platform. KstoneIt was initiated by the TKE team of Tencent cloud containerCloud native one-stop etcd governance project based on kubernetes。 The project originates […]