ajaxpro实现无刷新分页数据显示

原理: 封装了数据分页方法,提供2个参数:(每页显示多少条,页索引),然后字符串绘制数据显示,,,就不知道性能如何。。。

 

前台代码:

代码
 1 <body onload="JumpPage(5,1)" style="font-size:12px;" mce_style="font-size:12px;">
 2     <form id="form1" runat="server">
 3     <mce:script type="text/javascript" defer="defer"><!--
 4 //        //window.setInterval(display,10000);
 5 //        AjaxPro.onLoading = function(b) {
 6 //            var loading = document.getElementById("loadinfo");
 7 //            loading.style.visibility = b ? "visible" : "hidden";
 8 //        }
 9         function JumpPage(pageSize,pageindex)//分页处理方法 参数:每页显示多少条,页索引
10         {
11             $("#alist").html(Web.Manager.Test.GetString(parseInt(pageSize), parseInt(pageindex)).value);
12         }
13     
14 // --></mce:script>
15 
16     <div id="alist">
17     </div>
18     <div>
19         每页显示:<select id="Select1" onchange="JumpPage(this.value,1)" >
20             <option value="5" selected="selected">5条</option>
21             <option value="10">10条</option>
22              <option value="20">20条</option>
23               <option value="50">50条</option>
24                <option value="100">100条</option>
25         </select></div>
26     <div id="loadinfo" style="visibility: hidden;" mce_style="visibility: hidden;">
27         正在加载中.....</div>
28     <div>
29         <input type="button" onclick="JumpPage(1)" value="测试ajaxpro" /></div>
30     </form>
31 </body>
32 </html>

 

后台代码:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Dal;
using Model;
namespace Web.Manager
{
    
public partial class Test : System.Web.UI.Page
    {
        Big_DepDAL bdd 
= new Big_DepDAL();
        
protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(
typeof(Web.Manager.Test));
        }

  

        
protected DataTable GetList(int size, int pageindex )
        {
            
return bdd.Pager("B_id""desc""",size,pageindex);
        }

        
/// <summary>
        
/// 分页显示数据
        
/// </summary>
        
/// <param name="size">每页显示多少条 js默认显示</param>
        
/// <param name="pageindex">显示第几页 从第一页开始</param>
        
/// <returns></returns>
        [AjaxPro.AjaxMethod]
        
public string GetString(int size, int pageindex)
        {
            
string selectvalue="document.getElementById('Select1').value";
            StringBuilder text 
= new StringBuilder();
            text.Append(
"<table border='0' cellpadding='0' cellspacing='0' width='380px'>");
            text.Append(
"<tr align='center'>");
            text.Append(
"<td style='width:80px'>编号</td>");
            text.Append(
"<td style='width:300px'>名称</td>");
            text.Append(
"</tr>");
            DataTable dt 
= GetList(size, pageindex);
            
for (int i = 0; i < dt.Rows.Count; i++)
            {
                text.Append(
"<tr align='center'>");
                text.Append(
"<td style='width:80px'>" + dt.Rows[i]["B_id"].ToString() + "</td>");
                text.Append(
"<td style='width:300px'>" + dt.Rows[i]["Bd_name"].ToString() + "</td>");
                text.Append(
"</tr>");
            }
            text.Append(
"</table>");
            
int count=bdd.count("");
            
int pageCount = (int)(Math.Ceiling(count / (double)size));//获得总页数
            text.Append("<div align='center'>当前页:" + pageindex + "/" + pageCount + "  共"+count+"");

            
if (pageindex > 1//当前页不是第一页就显示上一页和首页
            {
                text.Append(
"<a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + ",1)>首页</a><a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + "," + (pageindex - 1+ ")>上一页</a>");
            }
            
if (pageindex < pageCount) //当前页小于总页就显示下一页和尾页
            {
                text.Append(
"<a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + "," + (pageindex + 1+ ")>下一页</a><a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + "," + (pageCount) + ")>尾页</a></div>");
            }

          
        
            
return text.ToString();







        }

        
    }
}

 

 

 

 

封装的Page方法:

 

 

代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Text;
 8 using System.Data;
 9 using Dal;
10 using Model;
11 namespace Web.Manager
12 {
13     public partial class Test : System.Web.UI.Page
14     {
15         Big_DepDAL bdd = new Big_DepDAL();
16         protected void Page_Load(object sender, EventArgs e)
17         {
18             AjaxPro.Utility.RegisterTypeForAjax(typeof(Web.Manager.Test));
19         }
20 
21   
22 
23         protected DataTable GetList(int size, int pageindex )
24         {
25             return bdd.Pager("B_id""desc""",size,pageindex);
26         }
27 
28         /// <summary>
29         /// 分页显示数据
30         /// </summary>
31         /// <param name="size">每页显示多少条 js默认显示</param>
32         /// <param name="pageindex">显示第几页 从第一页开始</param>
33         /// <returns></returns>
34         [AjaxPro.AjaxMethod]
35         public string GetString(int size, int pageindex)
36         {
37             string selectvalue="document.getElementById('Select1').value";
38             StringBuilder text = new StringBuilder();
39             text.Append("<table border='0' cellpadding='0' cellspacing='0' width='380px'>");
40             text.Append("<tr align='center'>");
41             text.Append("<td style='width:80px'>编号</td>");
42             text.Append("<td style='width:300px'>名称</td>");
43             text.Append("</tr>");
44             DataTable dt = GetList(size, pageindex);
45             for (int i = 0; i < dt.Rows.Count; i++)
46             {
47                 text.Append("<tr align='center'>");
48                 text.Append("<td style='width:80px'>" + dt.Rows[i]["B_id"].ToString() + "</td>");
49                 text.Append("<td style='width:300px'>" + dt.Rows[i]["Bd_name"].ToString() + "</td>");
50                 text.Append("</tr>");
51             }
52             text.Append("</table>");
53             int count=bdd.count("");
54             int pageCount = (int)(Math.Ceiling(count / (double)size));//获得总页数
55             text.Append("<div align='center'>当前页:" + pageindex + "/" + pageCount + "  共"+count+"");
56 
57             if (pageindex > 1//当前页不是第一页就显示上一页和首页
58             {
59                 text.Append("<a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + ",1)>首页</a><a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + "," + (pageindex - 1+ ")>上一页</a>");
60             }
61             if (pageindex < pageCount) //当前页小于总页就显示下一页和尾页
62             {
63                 text.Append("<a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + "," + (pageindex + 1+ ")>下一页</a><a href="javascript:JumpPage(" mce_href="javascript:JumpPage("" + selectvalue + "," + (pageCount) + ")>尾页</a></div>");
64             }
65 
66           
67         
68             return text.ToString();
69 
70 
71 
72 
73 
74 
75 
76         }
77 
78         
79     }
80 }
81 

 

 

posted @ 2010-07-23 17:43  xujun1860  阅读(705)  评论(0)    收藏  举报