在ASP.NET 中有很多数据展现的控件,比如用的最多的GridView,它同时也自带了分页的功能。但是我们知道用GridView来显示数据,如果没有禁用ViewState,页面的大小会是非常的大的。而且平时我们点击首页,下一页,上一页,尾页这些功能都是会引起页面回发的,也就是需要完全跟服务器进行交互,来回响应的时间,传输的数据量都是很大的。AJAX的分页可以很好的解决这些问题。
开发的坏境是:jQuery AJAX+Northwind。
具体的步骤:
SearchCustomer.aspx:
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript"> <BR>var pageIndex = 0; <BR>var pageSize = 10; <BR>$(function () { <BR>$("#btnSearch").click(function () { <BR>/* <BR>name 顾客的名字, 文本框中输入的内容 <BR>0 表示的是第1页 <BR>10 每页的大小 <BR>*/ <BR>var name = $("#txtSearch").val(); <BR>pageIndex = 0; <BR>AjaxGetData(name, pageIndex, pageSize); <BR>}); <BR>}); <BR>function AjaxGetData(name, index, size) { <BR>$.ajax({ <BR>url: "jQueryPaging.aspx", <BR>type: "Get", <BR>data: "Name=" + name + "&PageIndex=" + index + "&PageSize=" + size, <BR>dataType: "json", <BR>success: function (data) { <BR>var htmlStr = ""; <BR>htmlStr += "<table>" <BR>htmlStr += "<thead>" <BR>htmlStr += "<tr><td>CustomerID<td>CompanyName<td>ContactName<td>ContactTitle<td>Address<td>City" <BR>htmlStr += ""; <BR>htmlStr += "<tbody>" <BR>for (var i = 0; i < data.Customers.length; i++) { <BR>htmlStr += "<tr>"; <BR>htmlStr += "<td>" + data.Customers[i].CustomerID + "" <BR>+ "<td>" + data.Customers[i].CompanyName + "" <BR>+ "<td>" + data.Customers[i].ContactName + "" <BR>+ "<td>" + data.Customers[i].ContactTitle + "" <BR>+ "<td>" + data.Customers[i].Address + "" <BR>+ "<td>" + data.Customers[i].City + "" <BR>htmlStr += ""; <BR>} <BR>htmlStr += ""; <BR>htmlStr += "<tfoot>"; <BR>htmlStr += "<tr>"; <BR>htmlStr += "<td colspan='6'>"; <BR>htmlStr += "<span>共有记录" + data.Count + ";共<span id='count'>" + (data.Count % 10 == 0 ? parseInt(data.Count / 10) : parseInt(data.Count / 10 + 1)) + "页" + ""; <BR>htmlStr += "<a href='javascript:void' onclick='GoToFirstPage()' id='aFirstPage' >首 页 "; <BR>htmlStr += "<a href='javascript:void' onclick='GoToPrePage()' id='aPrePage' >前一页 "; <BR>htmlStr += "<a href='javascript:void' onclick='GoToNextPage()' id='aNextPage'>后一页 "; <BR>htmlStr += "<a href='javascript:void' onclick='GoToEndPage()' id='aEndPage' >尾 页 "; <BR>htmlStr += "<input type='text' /><input type='button' value='跳转' onclick='GoToAppointPage(this)' /> "; <BR>htmlStr += ""; <BR>htmlStr += ""; <BR>htmlStr += ""; <BR>htmlStr += ""; <BR>$("#divSearchResult").html(htmlStr); <BR>}, <BR>error: function (XMLHttpRequest, textStatus, errorThrown) { <BR>alert(XMLHttpRequest); <BR>alert(textStatus); <BR>alert(errorThrown); <BR>} <BR>}); <BR>} <BR>//首页 <BR>function GoToFirstPage() { <BR>pageIndex = 0; <BR>AjaxGetData($("#txtSearch").val(), pageIndex, pageSize); <BR>} <BR>//前一页 <BR>function GoToPrePage() { <BR>pageIndex -= 1; <BR>pageIndex = pageIndex >= 0 ? pageIndex : 0; <BR>AjaxGetData($("#txtSearch").val(), pageIndex, pageSize); <BR>} <BR>//后一页 <BR>function GoToNextPage() { <BR>if (pageIndex + 1 < parseInt($("#count").text())) { <BR>pageIndex += 1; <BR>} <BR>AjaxGetData($("#txtSearch").val(), pageIndex, pageSize); <BR>} <BR>//尾页 <BR>function GoToEndPage() { <BR>pageIndex = parseInt($("#count").text()) - 1; <BR>AjaxGetData($("#txtSearch").val(), pageIndex, pageSize); <BR>} <BR>//跳转 <BR>function GoToAppointPage(e) { <BR>var page = $(e).prev().val(); <BR>if (isNaN(page)) { <BR>alert("请输入数字!"); <BR>} <BR>else { <BR>var tempPageIndex = pageIndex; <BR>pageIndex = parseInt($(e).prev().val())-1; <BR>if (pageIndex < 0 || pageIndex >= parseInt($("#count").text())) { <BR>pageIndex = tempPageIndex; <BR>alert("请输入有效的页面范围!"); <BR>} <BR>else { <BR>AjaxGetData($("#txtSearch").val(), pageIndex, pageSize); <BR>} <BR>} <BR>} <BR></script>
数据的传输用的JSON格式。大家知道JSON是轻量级别的数据传输。前台的展现时用的table。这样生成的HTML代码很简洁。
HTML如下:
jQueryPaging.aspx页面的CS代码如下:
public partial class jQueryPaging : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Int32 pageIndex=Int32.MinValue;
Int32 pageSize=Int32.MinValue;
String name=String.Empty;
JavaScriptSerializer jss=new JavaScriptSerializer();
if(Request["Name"]!=null)
{
name=Request["Name"].ToString();
if (Request["PageIndex"] != null)
{
pageIndex = Int32.Parse(Request["PageIndex"].ToString());
pageSize = Request["PageSize"] != null ? Int32.Parse(Request["PageSize"].ToString()) : 10;
IList
Customer c = null;
DataSet ds= LookDataFromDB(name,pageIndex,pageSize);
foreach (DataRow row in ds.Tables[0].Rows)
{
c = new Customer();
c.CustomerID = row["CustomerID"].ToString();
c.CompanyName = row["CompanyName"].ToString();
c.ContactName = row["ContactName"].ToString();
c.ContactTitle = row["ContactTitle"].ToString();
c.Address = row["Address"].ToString();
c.City = row["City"].ToString();
customersLists.Add(c);
}
if (customersLists.Count>0)
{
Response.Write("{\"Count\":"+ds.Tables[1].Rows[0][0]+",\"Customers\":"+jss.Serialize(customersLists)+"}");
Response.End();
}
}
}
}
private DataSet LookDataFromDB(string name, int pageIndex, int pageSize)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SearchCustomerByName";
cmd.Parameters.Add(new SqlParameter("@name",name));
cmd.Parameters.Add(new SqlParameter("@pageIndex",pageIndex));
cmd.Parameters.Add(new SqlParameter("@pageSize", pageSize));
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
dataAdapter.Fill(ds);
}
catch (Exception)
{
}
finally
{
if (dataAdapter != null)
{
dataAdapter.Dispose();
}
if (cmd != null)
{
cmd.Dispose();
}
if (conn != null)
{
conn.Dispose();
}
}
return ds;
}
}
还有我们在CS中定义的Model类:
public class Customer
{
public String CustomerID { get; set; }
public String CompanyName { get; set; }
public String ContactName { get;set;}
public String ContactTitle { get; set; }
public String Address { get; set; }
public String City { get; set; }
}
SearchCustomerByName 存储过程的代码如下:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE SearchCustomerByName
@name nvarchar(30),
@pageIndex int,
@pageSize int
AS
BEGIN
SET NOCOUNT ON;
select t.CustomerID,t.CompanyName,t.ContactName,t.ContactTitle,t.Address,t.City from
(
select Row_Number() over (order by CustomerID) AS RowNum,* from Customers where ContactName like '%'+@name+'%'
) t
where t.RowNum between @pageIndex*10+1 and (@pageIndex+1)*10
select count(*) from Customers
where ContactName like '%'+@name+'%'
END
GO
具体的效果,大家可以把上述的代码响应的复制到VS中和数据库中,进行演示。
这个版本其实很多的功能点都是没有考虑到的,仅仅是个示例,大家可以在自己的实际项目中修改以上的功能来满足自己的需求。

















