页面无刷新分页

来源:互联网 发布:荷兰GPA算法 编辑:程序博客网 时间:2024/04/25 06:59
分页:页面无刷新源代码(由下面三部分组成)
一、前台代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script src="jquery-1.7.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $('#txtpagesize').val('5');
            var pageindex = 0;
            //获取总页数
            function GetCount() {
                ajax.WebService1.GetCount1(parseInt($('#txtpagesize').val()),onSuccess, onFailed);

            }


            function onSuccess(result) {
                totalpage = parseInt(result);
            }
            function onFailed() {
                alert('调用失败');
            }


            function GetData() {
                ajax.WebService1.DataBindArticlelist(parseInt($('#txtpagesize').val()),pageindex, onSuccess1, onFailed1);
            }
            function onSuccess1(result) {
                var table = '<table border=1>';
                table += '<tr><td>标题</td><td>类别</td><td>点击量</td><td>作者</td><td>出版日期</td></tr>'
                for (var i = 0; i < result.length; i++) {
                    table += '<tr>';
                    //字段名称必须和后台类中属性名称一致
                    table += '<td>' + result[i]["Title"] + '</td>' + '<td>' + result[i]["Classname"] + '</td>' + '<td>' + result[i]["Clickcount"] + '</td>' + '<td>' + result[i]["Publisher"] + '</td>' + '<td>' + result[i]["Published"] + '</td>';


                    table += '</tr>';
                }
                table += '</table>';
                $('#mydiv').html(table);
            }
            function onFailed1() {
                alert('调用失败');
            }


            $('#btnfirst').click(function () {


                pageindex = 1;
                $('#txtpageindex').val(pageindex);
                GetData();
            })
            $('#btnpre').click(function () {


                if (pageindex > 1) {
                    pageindex--;
                    $('#txtpageindex').val(pageindex);
                }


                GetData();
            })
            $('#btnnext').click(function () {


                if (pageindex < totalpage) {
                    pageindex++;
                    $('#txtpageindex').val(pageindex);
                }
                GetData();
            })
            //单击最后一页按钮时触发的事件
            $('#btnlast').click(function () {


                pageindex = totalpage;
                $('#txtpageindex').val(pageindex);
                GetData();
            })
            //单击Go按钮时触发的事件
            $('#btngo').click(function () {


                pageindex = $('#txtpageindex').val();
                GetData();
            })
            $('#btnok').click(function () {
                //根据当前设定获取总页数
                GetCount();
                //重新加载第一页
                $('#btnfirst').click();
            })


            //页面加载时获取总页数并显示第一页  
            GetCount();
            $('#btnfirst').click();
        })
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="WebService1.asmx" />
        </Services>
    </asp:ScriptManager>
    <div id="mydiv"></div>
    <div>
        <input id="btnfirst" type="button" value="第一页" /><input id="btnpre" type="button"
            value="上一页" /><input id="btnnext" type="button" value="下一页" /><input id="btnlast"
                type="button" value="最后一页" /><input id="txtpageindex" type="text" /><input id="btngo"
                    type="button" value="GO" /><br />
        设定每页显示条数<input id="txtpagesize" type="text" /><input id="btnok" type="button" 
            value="确定" /><br />
        
    </form>
</body>
</html>




二、web服务代码
namespace ajax
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
     [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        string strcon = ConfigurationManager.ConnectionStrings["strcon"].ConnectionString;
        private int pagesize = 5;


       
        //返回list
        [WebMethod]
        public List<articleinfo> DataBindArticlelist(int pagesizeuser, int pageindex)
        {
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "PROFENYE";
            //表明调用的是存储过程
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@PAGESIZE", pagesizeuser);
            cmd.Parameters.AddWithValue("@PAGEINDEX", pageindex);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            List<articleinfo> list = new List<articleinfo>();


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string title = Convert.ToString(dt.Rows[i]["TITLE"]);
                string classname = Convert.ToString(dt.Rows[i]["CLASSNAME"]);
                string clickcount = Convert.ToString(dt.Rows[i]["CLICKCOUNT"]);
                string publisher = Convert.ToString(dt.Rows[i]["PUBLISHER"]);
                string published = Convert.ToString(dt.Rows[i]["PUBLISHED"]);
                articleinfo article = new articleinfo()
                {
                    Title = title,
                    Classname = classname,
                    Clickcount = clickcount,
                    Publisher = publisher,
                    Published = published
                };
                list.Add(article);
            }


            cmd.Dispose();
            conn.Close();
            conn.Dispose();
            return list;
        }
        //根据用户设定每页显示的记录数量


        //获取总的页数
        [WebMethod]
        public string GetCount1(int pagesizeuser)
        {
            int totalpagesize = 0;
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select count(*) from ArticleInfo";
            conn.Open();
            object obj = cmd.ExecuteScalar();
            if (obj != null)
            {
                int totalrecord = Convert.ToInt32(obj);
                if (totalrecord % pagesize != 0)
                {
                    totalpagesize = totalrecord / pagesizeuser + 1;
                }
                else
                {
                    totalpagesize = totalrecord / pagesizeuser;
                }
            }
            cmd.Dispose();
            conn.Dispose();
            return totalpagesize.ToString();
        }
    }
}






三、创建类
namespace ajax
{
    public class articleinfo
    {
        private string title;


        public string Title
        {
            get { return title; }
            set { title = value; }
        }
        private string classname;


        public string Classname
        {
            get { return classname; }
            set { classname = value; }
        }
        private string clickcount;


        public string Clickcount
        {
            get { return clickcount; }
            set { clickcount = value; }
        }
        private string publisher;


        public string Publisher
        {
            get { return publisher; }
            set { publisher = value; }
        }
        private string published;


        public string Published
        {
            get { return published; }
            set { published = value; }
        }
    }
}
原创粉丝点击