Asp +Js 无刷新分页

Default.aspx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-3.1.1.js" type="text/javascript"></script>
    <link href="Style_Table.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        var pageSize = "10"; //每页行数 
        var currentPage = 1; //当前页 
        var totalPage = 1; //总页数 
        var params = ""; //参数
        $(document).ready(function () {//jquery代码随着document加载完毕而加载 
            //分页查询 
            queryForPages();

            //首页 
            $("#firstPage").click(function () {
                currentPage = "1";
                queryForPages();
            });
            //上一页 
            $("#previous").click(function () {
                if (currentPage > 1) {
                    currentPage--;
                }
                queryForPages();
            });
            //下一页 
            $("#next").click(function () {
                if (currentPage < totalPage) {
                    currentPage++;
                }
                queryForPages();
            });
            //尾页 
            $("#last").click(function () {
                currentPage = totalPage;
                queryForPages();
            });
            //GO
            $("#Button1").click(function () {
                currentPage = $("#Text2").val();
                if (currentPage <= totalPage) {
                    queryForPages();
                } else {
                }
            });
        });
        function queryForPages() {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: 'PagerHandler.ashx',
                data: 'currentPage=' + currentPage + '&pageSize=' + pageSize,
                success: function callbackFun(data) {

                    //清空数据 
                    clearDate();
                    fillTable(data);
                }

            });
        }
        //填充数据 
        function fillTable(info) {
            if (info.length > 0) {

                if (info[info.length - 1].Total % 10 == 0) {
                    totalPage = info[info.length - 1].Total / 10;
                } else {
                    totalPage = Math.ceil(info[info.length - 1].Total / 10);
                }

                $("#Text3").attr("value", '/' + totalPage);

                var tbody_content = ""; //不初始化字符串"",会默认"undefined" 
                for (var i = 0; i < info.length; i++) {

                    tbody_content +=
                        "<tr>" +
                        "<td data-title='序号' >" + info[i].Id + "</td>" +
                        "<td data-title='名称'>" + info[i].Name + "</td>" +
                        "<td data-title='年龄'>" + info[i].Age + "</td>" +
                        "<td data-title='性别'>" + info[i].Sex + "</td>" +
                        "<td data-title='地址'>" + info[i].Adress + "</td>" +
                        "</tr>";
                    $("#t_body").html(tbody_content);
                }
            }
            else {
                $("#t_head").html("");
                $("#t_body").html("<div style='height: 200px;width: 700px;padding-top: 100px;' align='center'>" + info.msg + "</div>");
            }
        }
        //清空数据 
        function clearDate() {
            $("#t_body").html("");
        }
    </script>
    <style type="text/css">
        #Text2
        {
            width: 28px;
        }
    </style>
</head>
<body>
    <div align="center" style="width: 100%;">
        <table style="text-align: center; width: 50%;" class="bordered">
            <thead id="t_head">
                <tr>
                    <th>
                        序号
                    </th>
                    <th>
                        名称
                    </th>
                    <th>
                        年龄
                    </th>
                    <th>
                        性别
                    </th>
                    <th>
                        地址
                    </th>
                </tr>
            </thead>
            <tbody id="t_body">
                <!-- ajax填充列表 -->
            </tbody>
        </table>
        <button id="firstPage">
            首页</button>
        <button id="previous">
            上一页</button>
        <button id="next">
            下一页</button>
        <button id="last">
            尾页</button>
        &nbsp;
        <input id="Text1" type="text" value="转到第几" readonly="readonly" style="border-style: none;
            width: 59px;" />
        <input id="Text2" type="text" />
        <input id="Text3" readonly="readonly" style="border-style: none; width: 38px;" type="text"
            value="/1" />
        <button id="Button1">
            GO</button>
    </div>
    <form id="form1" runat="server">
    </form>
</body>
</html>

 

Common.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public class Common
    {
        /// <summary>
        /// 连接字符串
        /// </summary>
        public static string DefaultConnectionString
        {
            get
            {
                return "Data Source=192.168.100.100;uid=sa;pwd=123321;Initial Catalog= Test";
            }
        }

        private static SqlParameter[] cmdPar;
        public static void setcmdPar(string strwhere, int pageindex)
        {
            cmdPar = new SqlParameter[]
            {
                new SqlParameter("@tblName", SqlDbType.VarChar),
                new SqlParameter("@strGetFields", SqlDbType.VarChar),
                new SqlParameter("@fldName", SqlDbType.VarChar),
                new SqlParameter("@PageSize", SqlDbType.Int),
                new SqlParameter("@PageIndex", SqlDbType.Int),
                new SqlParameter("@doCount", SqlDbType.Bit),
                new SqlParameter("@strWhere", SqlDbType.VarChar),
                new SqlParameter("@OrderType", SqlDbType.Bit)

            };
            cmdPar[0].Value = "[dbo].[T_Person_1] ";
            cmdPar[1].Value = "*,(select COUNT(*) from [dbo].[T_Person_1] where " + strwhere + ") as Total";
            cmdPar[2].Value = "Id";
            cmdPar[3].Value = 10;
            cmdPar[4].Value = pageindex;
            cmdPar[5].Value = 0;
            cmdPar[6].Value = strwhere;
            cmdPar[7].Value = 0;
        }

        public static BindingList<Person> GetlistpagePer(string sqlWhere, int pageindex)
        {
            setcmdPar(sqlWhere, pageindex);
            BindingList<Person> listper = new BindingList<Person>();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(DefaultConnectionString, CommandType.StoredProcedure, "GetListByPage", cmdPar))
            {
                while (reader.Read())
                {
                    Person per = new Person();

                    per.Id = Convert.ToInt32(reader["Id"]);
                    per.Name = Convert.ToString(reader["Name"]);
                    per.Age = Convert.ToString(reader["Age"]);
                    per.Sex = Convert.ToString(reader["Sex"]);
                    per.Adress = Convert.ToString(reader["Adress"]);
                    per.Total = Convert.ToInt32(reader["Total"]);

                    listper.Add(per);
                }
                reader.Close();
            }
            return listper;
        }

    }
}

 

Person.cs 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1
{
    public class Person
    {
        private int id;
        private string name;
        private string age;
        private string sex;
        private string adress;
        private int total;

 
        /// <summary>
        /// 序号
        /// </summary>
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 年龄
        /// </summary>
        public string Age
        {
            get { return age; }
            set { age = value; }
        }

        /// <summary>
        /// 性别
        /// </summary>
        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string Adress
        {
            get { return adress; }
            set { adress = value; }
        }

        /// <summary>
        /// 根据条件查询的总数
        /// </summary>
        public int Total
        {
            get { return total; }
            set { total = value; }
        }
    }
}

 

PagerHandler.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Web.Script.Serialization;

namespace WebApplication1
{
    /// <summary>
    /// PagerHandler 的摘要说明
    /// </summary>
    public class PagerHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            int pageIndex = Convert.ToInt32(context.Request["currentPage"]);
            int pagesize = Convert.ToInt32(context.Request["pageSize"]);
            if (pageIndex == 0)
            {
                pageIndex = 1;
            }

            string s = "1=1"; //条件

            BindingList<Person> perlist = new BindingList<Person>();

            perlist = Common.GetlistpagePer(s, pageIndex);
            string jsonStr = Newtonsoft.Json.JsonConvert.SerializeObject(perlist);//需要安装Json.Net
            context.Response.Write(jsonStr);
        }

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

 

CSS样式

 

table {
    border-collapse: collapse; /* IE7 and lower */
    border-spacing: 0;  
}

.bordered {
    border: solid #ccc 1px;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;
    -webkit-box-shadow: 0 1px 1px #ccc; 
    -moz-box-shadow: 0 1px 1px #ccc; 
    box-shadow: 0 1px 1px #ccc;         
}

.bordered tr:hover {
    background: #fbf8e9;
    -o-transition: all 0.1s ease-in-out;
    -webkit-transition: all 0.1s ease-in-out;
    -moz-transition: all 0.1s ease-in-out;
    -ms-transition: all 0.1s ease-in-out;
    transition: all 0.1s ease-in-out;     
}    
    
.bordered td, .bordered th {
    border-left: 1px solid #ccc;
    border-top: 1px solid #ccc;
    padding: 10px;
    text-align: left;    
}

.bordered th {
    background-color: #dce9f9;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
    background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:    -moz-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:     -ms-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:      -o-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:         linear-gradient(top, #ebf3fc, #dce9f9);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset; 
    -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;  
    box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;        
    border-top: none;
    text-shadow: 0 1px 0 rgba(255,255,255,.5); 
}

.bordered td:first-child, .bordered th:first-child {
    border-left: none;
}

.bordered th:first-child {
    -moz-border-radius: 6px 0 0 0;
    -webkit-border-radius: 6px 0 0 0;
    border-radius: 6px 0 0 0;
}

.bordered th:last-child {
    -moz-border-radius: 0 6px 0 0;
    -webkit-border-radius: 0 6px 0 0;
    border-radius: 0 6px 0 0;
}

.bordered th:only-child{
    -moz-border-radius: 6px 6px 0 0;
    -webkit-border-radius: 6px 6px 0 0;
    border-radius: 6px 6px 0 0;
}

.bordered tr:last-child td:first-child {
    -moz-border-radius: 0 0 0 6px;
    -webkit-border-radius: 0 0 0 6px;
    border-radius: 0 0 0 6px;
}

.bordered tr:last-child td:last-child {
    -moz-border-radius: 0 0 6px 0;
    -webkit-border-radius: 0 0 6px 0;
    border-radius: 0 0 6px 0;
}



/*----------------------*/

.zebra td, .zebra th {
    padding: 10px;
    border-bottom: 1px solid #f2f2f2;    
}

.zebra tbody tr:nth-child(even) {
    background: #f5f5f5;
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset; 
    -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;  
    box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;        
}

.zebra th {
    text-align: left;
    text-shadow: 0 1px 0 rgba(255,255,255,.5); 
    border-bottom: 1px solid #ccc;
    background-color: #eee;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#eee));
    background-image: -webkit-linear-gradient(top, #f5f5f5, #eee);
    background-image:    -moz-linear-gradient(top, #f5f5f5, #eee);
    background-image:     -ms-linear-gradient(top, #f5f5f5, #eee);
    background-image:      -o-linear-gradient(top, #f5f5f5, #eee); 
    background-image:         linear-gradient(top, #f5f5f5, #eee);
}

.zebra th:first-child {
    -moz-border-radius: 6px 0 0 0;
    -webkit-border-radius: 6px 0 0 0;
    border-radius: 6px 0 0 0;  
}

.zebra th:last-child {
    -moz-border-radius: 0 6px 0 0;
    -webkit-border-radius: 0 6px 0 0;
    border-radius: 0 6px 0 0;
}

.zebra th:only-child{
    -moz-border-radius: 6px 6px 0 0;
    -webkit-border-radius: 6px 6px 0 0;
    border-radius: 6px 6px 0 0;
}

.zebra tfoot td {
    border-bottom: 0;
    border-top: 1px solid #fff;
    background-color: #f1f1f1;  
}

.zebra tfoot td:first-child {
    -moz-border-radius: 0 0 0 6px;
    -webkit-border-radius: 0 0 0 6px;
    border-radius: 0 0 0 6px;
}

.zebra tfoot td:last-child {
    -moz-border-radius: 0 0 6px 0;
    -webkit-border-radius: 0 0 6px 0;
    border-radius: 0 0 6px 0;
}

.zebra tfoot td:only-child{
    -moz-border-radius: 0 0 6px 6px;
    -webkit-border-radius: 0 0 6px 6px
    border-radius: 0 0 6px 6px
}

 

数据库的访问类(http://www.cnblogs.com/haibing0107/p/6141242.html)以及分页的存储过程(http://www.cnblogs.com/haibing0107/p/5527205.html)都可以在这些地址上找得到。

 

转载于:https://www.cnblogs.com/haibing0107/p/6274305.html

Asp+AJAX静态分页 亲测 可用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>Asp+AJAX静态分页</title> <style type="text/css"> <!-- body { text-align:center;font:14px Verdana,sans-serif; } a:link,a:visited { color:#00f;text-decoration:none; } a:hover { color:#f00;text-decoration:underline; } #main { width:450px;background:#f2f2f2;border:1px #999 solid;padding:10px;text-align:left;line-height:150%;margin:0 auto; } #title { width:100%;line-height:30px;border-bottom:1px #999 solid;display:table; } #left { float:left;width:50%;text-align:left;font-size:14px;font-weight:bold; } #right { float:left;width:50%;text-align:right; } #content { width:100%;margin:10px 0;clear:both; } #download { width:100%;margin:10px 0;line-height:150%; } --> </style> [removed] <!-- function createAjax() { //该函数将返回XMLHTTP对象实例 var _xmlhttp; try { _xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); //IE的创建方式 } catch (e) { try { _xmlhttp=new XMLHttpRequest(); //FF等浏览器的创建方式 } catch (e) { _xmlhttp=false; //如果创建失败,将返回false } } return _xmlhttp; //返回xmlhttp对象实例 } function getweblist(page) { //该函数用来获取分页数据 var xmlhttp=createAjax(); //创建变量xmlhttp,并将createAjax()函数创建的对象实例赋于它 if (xmlhttp) { //如果xmlhttp对象创建成功,则执行条件语句中的程序 var content=document.getElementById('content'); //获取页面中id为content的对象 xmlhttp.open('get','server.asp?page='+page+'&n='+Math.random(),true); //打开与服务器的连接,其中get为连接方式,server.asp为要连接的页面,有两个参数,其中第一个参数page为需要返回数据的页数,第二个参数n为一个随机数,这样每次发送的URL都会不一样,相当于都向服务器发出一个新的请求,避免浏览器缓存数据。 xmlhttp.onreadystatechange=function() { //为xmlhttp对象的readyState属性指定事件,改属性值改变时,则会执行其中的程序 if (xmlhttp.readyState==4 && xmlhttp.status==200) { //如果xmlhttp.readyState==4并且xmlhttp.status==200时,执行条件中的程序,其中readyState有五个值,4为请求完成,是客户端向服务器提交的数据成功到达,status有N多值-_-!!,其中200为OK,是指服务器向客户端完成发送数据。 content[removed]=unescape(xmlhttp.responseText); //将服务器返回的数据解码并写入指定的ID中。 } else { content[removed]='<span>正在从服务器提取数据......</span>'; //如果服务器没有完成传送,则向用户提示正在传输。 } } xmlhttp.send(null); //向服务器发送请求,因为是get请求,会直接附在URL后面,所以这里括号中的数据为null,IE中也可以不写,但FF就必须加上null,否则会发送失败。 } } function edit() { //编辑分页显示条数的函数 var str='<form style="margin:0">每页显示 <input type="text" id="pagesize" size="3"> 条 <input type="button" id="savebtn" value="保存" onclick="save()"> <input type="button" id="cancelbtn" value="取消" onclick="rightinfo()"></form>' //定义html字符串 var right=document.getElementById('right'); //获得页面中的right对象。 right[removed]=str; //将str变量的值写入该对象中。 } function rightinfo() { //right对象中的原始信息,请在页面开始和被显示条数被修改后调用 document.getElementById('right')[removed]='<a href="[removed]void(edit())" title="修改每页显示条数">Edit</a>'; } function save() { //保存修改后的显示条数 var pagesize=document.getElementById('pagesize'); //这个就不写了,跟上面的用法一样。 if (pagesize.value==''||/[0-9]+/.test(pagesize.value)==false) { //确定用户输入的新数据是不是一个数字 alert("请正确填写每页显示条数! "); return; } var xmlhttp=createAjax(); //创建对象 if (xmlhttp) { xmlhttp.open('get','set.asp?pagesize='+pagesize.value+'&n='+Math.random(),true) //参上同看 xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById('right')[removed]=unescape(xmlhttp.responseText); //先写入从服务器返回的字符串,如果成功,会写入completed。 getweblist(1); //从新获取新修改后的第一页的数据 setTimeout('rightinfo()',3000); //3秒后将right对象的原始字符串写入。 } else { document.getElementById('pagesize').disabled=true; //将几个FORM表单的元素都设为不可改动 document.getElementById('savebtn').disabled=true; document.getElementById('cancelbtn').disabled=true; } } xmlhttp.send(null); //发送请求。 } } //--> [removed] </head> <body> <div id="main"> <div id="title"> <div id="left">Asp+AJAX静态分页</div> <div id="right"></div> </div> <div id="content"></div> </div> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值