The Service side is a paging class of text files. It is mainly processed in the stream. Of course, I have used the </br> keyword on the Internet for paging.
I personally think that not all the time can meet the requirements. If I write this on myself, I still spend some time, mainly because I am too stupid and have a poor foundation. I hope you can give 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.txt Path = _ txtPath;
This. pageSize = _ Pagesize;
}
}
String txtPath; // file path
Int pageSize; // number of lines 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.txt Path );
String line;
Int count = 0;
While (line = sr. ReadLine ())! = Null)
{
// Line + = "fuck ";
Count ++;
}
Return count;
}
Public string ReadTxtToHtml ()
{
String line; // stores a line of text
Int ptr = 0; // row count
Int ttp = 1; // the maximum number of pages after the page
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 & gt; 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 usage of the paging class. It is not intuitive. It is mainly because when I write the code, I only need 6 paging files; you can also meet multiple requirements.
I am too lazy to write comments. I registered such a long blog Park to write so many bad things. It is far from your 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 determines 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, but it is still rough to write. I hope the experts will not throw my eggs.
Ajax code
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 easy to understand, because I will not write deep code. It is important for us to practice it. Cainiao are working hard. I will get a lot from this example.
At least when I was a novice cainiao, it would be very fast to see this example.
The following is the display part. This is a complete example. Be honest and be in charge
Copy codeThe Code is as follows:
<Table width = "90%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<Td align = "center"> ice cream production methods </td>
</Tr>
<Tr>
<Td id = "article" align = "left" style = "font-size: 12px; height: 805px"> td>
</Tr>
<Tr>
<Td id = "Count1" style = "height: auto; text-align: right"> </td>
</Tr>
</Table>
Finished !! I hope it will not disappoint people who come to view my code.