Asp.net+ajax text file static paging implementation code _AJAX related

Source: Internet
Author: User
Tags readline
The service-side section, a text-file paging class. Mainly processed in the stream. Of course, I have seen the Web page with </br> keywords for pagination
Personally feel not all the time can meet the requirements, the one himself wrote this, or a little time, mainly because I was too stupid, the foundation is very poor. I hope everyone has a better idea.
Copy Code code 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;//one line of text
int ptr = 0;//row count
int TTP = maximum number of pages after paging 1;//
StreamReader sr = new StreamReader (Txtpath);
String htmlstr = "";//for storing HTML code
Htmlstr = "#" + TTP + "</br>";
while (line = Sr. ReadLine ())!= null)
{
if (ptr = = pageSize)
{
ttp++;
Htmlstr = "#" + TTP + "</br>";
ttp++;
Htmlstr = "#" + TTP + "</br>";
ptr = 0;
}
Htmlstr + + "</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's how to use this class:
This code is used to explain the use of pagination class is not intuitive, mainly written when I was for many file paging, fortunately I only need 6 here, need multiple also can meet the requirements.
Hehe is not perfect, the annotation part is lazy to write, so did not write, hey is in is too lazy. It took so long to register a blog park to write so few rotten things. I still want to go far with my original intention.
Copy Code code 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 =
Front end to determine if 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];
}
}

Above this is also for the AJAX request to prepare, write or very rough, hope Master mo throw my eggs
Ajax Code Section
Copy Code code 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 easy to understand, hehe, because I will not write advanced code. The important thing is that we all have to practice. The rookie try a lot of OH. I'll get a lot out of this example level.
At least when I was a junior rookie, if I had this example, it would be a quick step.
The following is the part of the display. For everyone to finish the whole. The person should be honest and responsible
Copy Code code as follows:

<table width= "90%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
<TD align= "center" > Ice cream Making method Daquan </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>

It's done!! Hopefully, the people who come to see my code will not be disappointed.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.