码迷,mamicode.com
首页 > Web开发 > 详细

ajax分页

时间:2017-04-16 16:56:49      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:hang   lin   ids   models   url   summary   ntb   mode   line   

MVC cshtml:

第一次加载:

@*信息*@
  <div id="f">
        @{ List<know> klist = new knowdata().select1();
           foreach (know k in klist)
           {
            <div style="width: 100%; height: 30px; line-height: 30px; margin-top: 10px; cursor: pointer;" onclick="change(@k.ids)">
                <div style="float: left">@k.title</div>
                <div style="float: right">@k.ktime</div>
            </div>
           }
        }
    </div>

分页按钮设置:

 @*分页*@
    <div style=" margin-left: 70%;position:relative;height:50px;">
        当前第<span id="sp-number">1</span><input type="button" class="btn btn-primary" value="上一页" id="btn1" />
        <input type="button" class="btn btn-primary" value="下一页" id="btn2" />

        @{
            List<know> klist1 = new knowdata().select();
            int cd = Convert.ToInt32(Math.Ceiling(klist1.Count() * 1.0 / 5));
        
            @:共<label id="lab1">@cd</label>页
        }
    </div>

js里:

<script>

    var pageCount = 5;
    var pageNumber = 1;
    //上一页
    document.getElementById("btn1").onclick = function () {
        pageNumber--;
        if (pageNumber < 1) {

            pageNumber = 1;
        }
        else {
            Load(pageCount, pageNumber);
        }
    };
    //下一页
    document.getElementById("btn2").onclick = function () {
        pageNumber++;
        var ss = $("#lab1").text();
        if (pageNumber > ss) {
            pageNumber = ss;
        }
        else {
            Load(pageCount, pageNumber);
        }
    };
    //分页加载
    function Load(count, number) {
        $.ajax({
            url: "/ajax/Kload.ashx",
            data: { "count": count, "number": number },
            type: "post",
            dataType: "json",
            success: function (data) {
                document.getElementById("f").innerHTML = "";
                for (i in data) {
                    var end = "<div style=\"width: 100%; height: 30px; line-height: 30px; margin-top: 10px;cursor:pointer;\" onclick=\"change(" + data[i].ids + ")\">";
                    end += "<div style=\"float: left\">" + data[i].con + "</div>";
                    end += "<div style=\"float: right\">" + data[i].ntime + "</div>";
                    end += "</div>";

                    document.getElementById("f").innerHTML += end;
                }
                document.getElementById("sp-number").innerHTML = number;

            }
        });
    };

ajax里:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using aihu.Models;
namespace aihu.ajax
{
    /// <summary>
    /// Kload 的摘要说明
    /// </summary>
    public class Kload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            List<know> klist = null;
            int c = Convert.ToInt32(context.Request["count"]);
            int n = Convert.ToInt32(context.Request["number"]);
            string json = "[";
            using (aihuDataContext con = new aihuDataContext())
            {
                //int cou= Convert.ToInt32(Math.Ceiling(con.news.Count() * 1.0 / c));
                //if (n > cou)
                //{ nlist = null; }
                //else
                //{
                klist = con.know.Skip((n - 1) * c).Take(c).ToList();
                //}
            }
            int count = 0;
            foreach (know k in klist)
            {
                if (count > 0)
                { json += ","; }
                json += "{\"con\":\"" + k.title + "\",\"ntime\":\"" + k.ktime + "\",\"ids\":\"" + k.ids + "\"}";
                count++;
            }
            json += "]";
            context.Response.Write(json);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 

ajax分页

标签:hang   lin   ids   models   url   summary   ntb   mode   line   

原文地址:http://www.cnblogs.com/wy1992/p/6719162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!