注册 登录
编程论坛 jQuery论坛

ajax实现分页,只能在载入时执行一次

gxfsx 发布于 2017-01-02 01:20, 3262 次点击
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]
1 回复
#2
heathcliffhu2017-02-16 20:51
把response.write(...) 改成 response.getWriter().print(...),
如果还是不行,试试把response.end()删掉
1