| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3273 人关注过本帖
标题:ajax实现分页,只能在载入时执行一次
只看楼主 加入收藏
gxfsx
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2012-6-29
结帖率:0
收藏
 问题点数:0 回复次数:1 
ajax实现分页,只能在载入时执行一次
ajax实现分页,只在页面载入时显示第一页,再点下一页没反应,不懂为什么?请大侠帮看看,代码如下:
前台代码
程序代码:
 <script src="js/jquery.1.11.3.min.js"></script>
    <script type="text/javascript">
        var pageIndex = 0;
        var pageSize = 5;
        var pageCount = 0;
        var recordCount = 0;
        AjaxGetData(pageIndex, pageSize);
        function AjaxGetData(index, size) {
            $.ajax({
                url: "index.aspx",
                type: "post",
                data: "pageindex=" + index + "&pagesize=" + size + "&action=ajax&tm=" + new Date(),
                dataType: "json",
                //cache:false,
                success: function (data) {
                    var htmlStr = "";
                    htmlStr += "<table width=100%>";
                    for (var i = 0; i < data.length; i++) {
                        htmlStr += "<tr><td  class='rr' onmouseover='javascript:onOver(this)' onmouseout='javascript:onOut(this)'onclick='javascript:onDown(this);'>";
                        htmlStr += "<a href='index.aspx?id=" + data[i].AdminID + "' class='cpx12huei' target='content'>";
                        htmlStr += data[i].Manager ;
                        htmlStr += "</a>";
                        htmlStr += "</td></tr>";
                    }
                    htmlStr += "<tr style='text-align:center;'>";
                    htmlStr += "<td>";
                    recordCount = Number(data.length);
                    pageCount = Math.ceil(recordCount / pageSize);
                    htmlStr += "共" + recordCount + "条记录&nbsp;&nbsp;共<span id='count'>" + pageCount + "</span>页&nbsp;&nbsp;&nbsp;&nbsp;";
                    htmlStr += "<a href='javascript:void' onclick='GoToPrePage()' id='aPrePage' >前一页</a>&nbsp;&nbsp; ";
                    htmlStr += "<a href='javascript:void' onclick='GoToNextPage()' id='aNextPage'>后一页</a>&nbsp;&nbsp; ";
                    htmlStr += "</td>";
                    htmlStr += "</tr>";
                    htmlStr += "</table>";
                    $("#div_menu").html(htmlStr);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest);
                    alert(textStatus);
                    alert(errorThrown);
                }
            });
        }
        //前一页
        function GoToPrePage() {
            pageIndex -= 1;
            if (pageIndex < 1) {
                pageIndex = 1;
                return;
            }
            AjaxGetData(pageIndex, pageSize);
        }
        //后一页
        function GoToNextPage() {
            pageIndex += 1;
            if (pageIndex > pageCount) {
                pageIndex = pageCount;
                return;
            }
            AjaxGetData(pageIndex, pageSize);
        }

    </script>


后台代码
程序代码:
        BLLData bll = new BLLData();
        private string _action = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["action"] != null)
                this._action = Request["action"];
            if (_action == "ajax")
                {
                    Int32 pageIndex = Int32.MinValue;
                    Int32 pageSize = 5;
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    if (Request["pageindex"] != null)
                    {
                        pageIndex = Int32.Parse(Request["pageindex"].ToString());
                    }
                    else
                        pageIndex = 1;
                    pageSize = Request["pagesize"] != null ? Int32.Parse(Request["pagesize"].ToString()) : 10;

                    IEntity entity = (IEntity)Activator.CreateInstance(Type.GetType(DataTool.SetEntityName("admin"), false, true));

                    int recordCount = bll.GetCount(entity);
                    int pageCount = (int)Math.Ceiling(((double)recordCount) / ((double)pageSize));

                    //处理接收到的数据
                    int start = 0;
                    int end = 0;

                    if (pageIndex > pageCount)
                        pageIndex = pageCount;
                    else if (pageIndex < 1)
                        pageIndex = 1;

                    start = (pageIndex - 1) * pageSize + 1;
                    end = pageIndex * pageSize;

                    IList<Admin> list = new List<Admin>();
                    Admin md = new Admin();
                    DataSet ds = bll.ExecuteDataSet(md, start, end);
                    md = null;
                    Admin model = null;
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        model = new Admin();
                        model.AdminID = int.Parse(ds.Tables[0].Rows[i]["adminID"].ToString());
                        model.Manager = ds.Tables[0].Rows[i]["manager"].ToString();
                        model.Pwd = ds.Tables[0].Rows[i]["pwd"].ToString();
                        list.Add(model);
                    }
                    if (list.Count > 0)
                    {
                        Response.Write(jss.Serialize(list));
                    }
                    else
                    {
                        Response.Write("{\"Count\":0,\"operationModel\":null}");
                    }
                    Response.End();
                }
       
        }

[attach]87998[/attach]
搜索更多相关主题的帖子: javascript function ajax 前台 
2017-01-02 01:20
heathcliffhu
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-2-16
收藏
得分:0 
把response.write(...) 改成 response.getWriter().print(...),
如果还是不行,试试把response.end()删掉
2017-02-16 20:51
快速回复:ajax实现分页,只能在载入时执行一次
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.089629 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved