在Web显示的时候我们经常会遇到分页显示,而网上的分页方法甚多,但都太过于消耗带宽,所以我想到了用Ajax来分页,利用返回的Json来处理返回的数据,

大大简化了带宽的压力。

先说下思路,无非就是异步执行ajax 把新列表所需要的数据用json格式返回来,然后把原来的列表Remove掉,让新列表Show。

上前台Html代码:

<!--一个table 里面是repeater 当然了你也可以用<UL>那样效率更高-->

<table width="100%" border="0" cellpadding="0" id = "newslist" cellspacing="1" bgcolor="b5d6e6">
            <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
            <tr>
            <td width="15%" height="22" background="images/bg.gif" bgcolor="#FFFFFF"><div align="center">
             <a id="quan">全选</a>
            <a id="fan">反选</a>
            </div></td> 
             <td width="12%" height="22" background="images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">接收号码</span></div></td>
            <td width="14%" height="22" background="images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">发送时间</span></div></td>
            <td width="18%" background="images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">邮件地址</span></div></td>
            <td width="23%" height="22" background="images/bg.gif" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">内容</span></div></td>
            <td width="15%" height="22" background="images/bg.gif" bgcolor="#FFFFFF" class="STYLE1"><div align="center">基本操作</div></td>
          </tr>
            </HeaderTemplate>
           <ItemTemplate>
             <tr class="item">
            <td height="20" bgcolor="#FFFFFF"><div align="center">
              <input class ="check_select" type="checkbox" name="checkbox2" title =<%#Eval("Id")%> value="checkbox" />
            </div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1"><%#Eval("Name")%></span></div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">2007-11-16 15:00:20 </span></div></td>
            <td bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">tiezhu0902@163.com</span></div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE1">南京科技股份有限公司…</span></div></td>
            <td height="20" bgcolor="#FFFFFF"><div align="center"><span class="STYLE4"><img src="data:images/edt.gif" width="16" height="16" />编辑&nbsp; &nbsp;<img src="data:images/del.gif" width="16" height="16" />删除</span></div></td>
          </tr>
           </ItemTemplate>
            </asp:Repeater>
        </table>

<!--一个table 里面是上一页,下一页,当前页面信息等等……当然了你也可以用<UL>那样效率更高-->

<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td class="STYLE4"><a id="delete"  style =" cursor :hand;">批量删除</a>

</td>
             <td class="STYLE4">共有<asp:Label ID="Count" runat="server" Text=""></asp:Label>条数据,
             每页显示<asp:Label ID="ItemCount" runat="server" Text=""></asp:Label>条,
             当前<asp:Label ID="index" runat="server" Text=""></asp:Label>
             /
             <asp:Label ID="PageCount" runat="server" Text=""></asp:Label>
             页

</td>
            <td><table border="0" align="right" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="40"><img src="data:images/first.gif" class="page" title ="first" width="37" height="15" /></td>
                  <td width="45"><img src="data:images/back.gif" class="page" title ="back" width="43" height="15" /></td>
                  <td width="45"><img src="data:images/next.gif" class="page" title ="next" width="43" height="15" /></td>
                  <td width="40"><img src="data:images/last.gif" class="page" title ="last" width="37" height="15" /></td>
                  <td width="100"><div align="center"><span class="STYLE1">转到第
                      <asp:DropDownList ID="Drop" runat="server">
                      </asp:DropDownList>                   
                    页 </span></div></td>
                  <td width="40"><img class="page" title = "go" src="data:images/go.gif" width="37" height="15" /></td>
                </tr>
            </table> 
            </td>
          </tr>
        </table>

好了,初步的html页面布局已经做完了,下来我们该做页面和服务器的交互了

页面引入Jquery,为上一页,下一页,首页,尾页,增加Click事件

设置他们的Class = page 以便于给他们增加Click

$(".page").click(function () {
                var type = $(this).attr("title");
                var thisindex = $("#index").text(); //获取当前页码
                switch (type) {
                    case 'first':
                        {
                            pageindex = 1;
                            PageIndex(1);  //Ajax 回传函数
                            return;
                        }
                    case 'back':
                        {
                            if (thisindex == 1) {
                                alert("当前已经是第一页!");
                            }
                            else {
                                pageindex = parseInt(thisindex) - 1;
                                PageIndex(pageindex);
                            }
                            return;
                        }
                    case 'next':
                        {
                            if (thisindex == $("#Drop").children().length) {
                                alert("当前已经是最后页!");
                            }
                            else {
                                pageindex = parseInt(thisindex) + 1;
                                PageIndex(pageindex);
                            }
                            return;
                        }
                    case 'last':
                        {
                            var max = $("#Drop").children().length;
                            pageindex = max;
                            PageIndex(max);
                            return;
                        }
                    case 'go':
                        {
                            var _go = $("#Drop").val();
                            pageindex = _go;
                            PageIndex(_go);
                            return;
                        }
                }

})

下面的代码在是Ajax的回传封装,切记在$(function(){ ....})之外

var pageindex;
        function PageIndex(index) {
            $.ajax({
                type: "Post", //回传格式
                url: "WebForm1.aspx", //回传页面
                data: { "id": index },  //回传参数表示请求的是第几页
                cache: false, //禁止使用浏览器缓存
                //dataType: "json",
                success: function (data) {
                    var json = eval(data); //把返回来的数据 json
                    var str = "";
                    for (var i = 0; i < json.items.length - 1; i++) {
                        str = str + "<tr class='item' ><td height='20' bgcolor='#FFFFFF'><div align='center'><input class ='check_select' type='checkbox' name='checkbox2' title =" + json.items[i].id.toString() + " value='checkbox' /></div></td>";
                        str = str + "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>" + json.items[i].name + "</span></div></td><td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>2007-11-16 15:00:20 </span></div></td>";
                        str = str + "<td bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>tiezhu0902@163.com</span></div></td><td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE1'>南京科技股份有限公司…</span></div></td>";
                        str = str + "<td height='20' bgcolor='#FFFFFF'><div align='center'><span class='STYLE4'><img src='images/edt.gif' width='16' height='16' />编辑&nbsp; &nbsp;<img src='images/del.gif' width='16' height='16' />删除</span></div></td></tr>";
                    }
                    $("#index").text(pageindex); //设置当前的页码
                    $(".item").remove(); //移除原有的列表
                    $("#newslist").append(str); //显示返回的列表
                },
                error: function () {
                    alert("服务错误");
                }
            })
        }

下面才C#代码了:

void data(int id,bool b)
        {
            int listcount = 7; //设置页面的显示数量
            LinqToSqlDataContext ds = new LinqToSqlDataContext();
            var q = (from a in ds.Admin select a).Skip((id-1) * listcount).Take(listcount); //获取请求页面的列表
            if (!b)  //如果是页面初始加载 默认显示
            {
                Repeater1.DataSource = q;
                DataBind();
                int count = (from a in ds.Admin select a).ToList().Count;
                this.Count.Text = count.ToString();                
                count = count / listcount + (count % listcount > 0 ? 1 : 0);
                this.ItemCount.Text = listcount.ToString();
                this.index.Text = "1";
                this.PageCount.Text = count.ToString();
                for (int i = 0; i < count; i++)
                {
                    Drop.Items.Add((i + 1).ToString());
                }
            }
            else
            {
                string str = "({items:[";                
                foreach (var a in q)
                {
                    str += "{'id':'" + a.Id + "'";
                    str += ",'name':'" + a.Name+"'";
                    str += "},";
                }
                str += "]})";
                Response.Write(str);
                Response.End();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                data(1,false);
            }
            PageIndex();
        }
        void PageIndex()
        {
            if (Request["id"] != null)
            {
                int id = int.Parse(Request["id"].ToString());
                data(id,true);
            }
        }

好了,到此,大功告成,后续会把批量删除(批量删除后的页面变化等等加入),

利用Linq + Jquery + Ajax 异步分页的实现的更多相关文章

  1. 5.Hibernate实现全套增删改查和ajax异步分页

    1.1 创建如下oracle数据库脚本 drop sequence seq_stu; create sequence SEQ_STU minvalue maxvalue start increment ...

  2. Jquery Ajax 异步设置Table中某列的值

    可根据table中某列中的ID去改变某列的值! 只是参考,实际应用中不能这样做的,如果有很多行,频繁访问服务器,服务器是顶不住的! JS: $(document).ready(function () ...

  3. 使用 jQuery Ajax 异步登录,并验证用户输入信息(maven)

    使用 jQuery Ajax 异步登录,并验证用户输入信息(maven) 本篇内容: (1)上一篇是使用同步的请求实现登录,并由 Servlet 决定登陆后下一步做哪些事情,本篇使用 jQuery A ...

  4. 2018.2.28 PHP中使用jQuery+Ajax实现分页查询多功能如何操作

    PHP中使用jQuery+Ajax实现分页查询多功能如何操作 1.首先做主页Ajax_pag.php 代码如下 <!DOCTYPE html> <html> <head& ...

  5. jquery Ajax异步请求之session

    写了一个脚本,如下: $(function () { $("#btnVcode").click(function () { var receiveMobile = $(" ...

  6. jQuery Ajax(异步请求)

    jQuery异步请求 原始的异步请求是需要创建的 XMLHttpRequest 对象.(IE5,6不支持)目前很多浏览器都支持XMLHttpRequest对象 jQuery ajax常用的回调函数:b ...

  7. jQuery ajax 实现分页 kkpager插件

    代码片段一: <!--分页组件 JS CSS 开始--> <!--分页组件 CSS--> <link type="text/css" href=&qu ...

  8. jquery ajax异步调用

    写程序的第一步都要知其然,至于知其所以然就要看个人的爱好了.下面说一下web开发中经常用的ajax. 这里是用的jquery框架实现的ajax异步调用.废话少说先上代码.(asp.net开发) var ...

  9. jquery.ajax异步发送请求的简单测试

    使用ajax异步发送请求到一般处理程序,判断输入的用户名和密码 1.添加Html页面,导入jquery 2.编写js代码和页面标签 <script type="text/javascr ...

随机推荐

  1. jquery跨域调用wcf

    使用jquery跨域调用wcf服务的时候会报如下错误 $.ajax({ url: 'http://localhost:28207/Service1.svc/GetData', method: 'get ...

  2. Linq操作

    Linq使用Group By 1 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述 ...

  3. 创建表 添加主键 添加列常用SQL语句

    --删除主键 alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段 ...

  4. 263. Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  5. 1008. Elevator (20)

    The highest building in our city has only one elevator. A request list is made up with N positive nu ...

  6. EMVTag系列16《AC响应数据》

    在一个联机交易中,要传送到发卡行的专有应用数据. 字段 长度(字节) 赋值 说明 长度 1 07 分散密钥索引 1 00 密文版本号 1 01 根据发卡行密钥版本设置 卡片验证结果(CVR) 4 03 ...

  7. hdu 1195 Open the Lock

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...

  8. C#.Net 图片处理大全

    C# How to: Image filtering by directly manipulating Pixel ARGB values C# How to: Image filtering imp ...

  9. sql server 查询数据库所有的表名+字段

    SELECT * FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='Account' SELECT    (case when a.colorder= ...

  10. SqlParameter[] 添加

    protected void Button1_Click(object sender, EventArgs e) { string strSql="insert into Invoice_R ...