zoukankan      html  css  js  c++  java
  • Ajax文本文件静态分页分页

    服务端部分 ,文本文件分页的类。主要在流中处理。当然我看过网上的用</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"]));
            
    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 是否为数字
                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 的请求做准备的,写的还是很粗糙的,希望高手莫扔我鸡蛋

    Ajax 代码部分

     

    代码
    <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(
    "您的的浏览器不支持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>

    代码还是很浅显易懂的,呵呵因为我也不会写高深的代码。重要的我们都要动手去实践。菜鸟们多多努力哦。做完我这个例子级会收获很多哦。

    至少当我还是个初级菜鸟的时候要是有这个例子进步会很快。

    下面就是显示的部分了。给大家一个完完整整的。做人要厚道要负责人

    代码
    <table width="90%" border="0" cellspacing="0" cellpadding="0">
              
    <tr>
                
    <td align="center">冰激凌制作方法大全</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>

    完成了!!希望会让来看我代码的人不会失望。

    完成效果

  • 相关阅读:
    cmd 新建安卓工程
    新创建的android工程里面没有activity问题解决
    背景色横向渐变css5
    Serialable与Parcelable
    Observer观察者模式
    Linux基础(1)
    android 出现Make sure the Cursor is initialized correctly before accessing data from it
    android项目中导入actionbarsherlock 需要注意的地方
    android BadgeView的使用(图片上的文字提醒)
    android仿系统Launcher界面,实现分屏,左右滑动效果(ViewSwitcher)
  • 原文地址:https://www.cnblogs.com/cestbon/p/Ajax.html
Copyright © 2011-2022 走看看