您好,登录后才能下订单哦!
本篇文章给大家分享的是有关Asp.net中怎么利用jquery和.ashx文件实现分页,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
首先是页面,因为是要实践思路,所以页面真是很简单。引用了jquery.js 
复制代码
<div id="lab"> 
<input id="Button1" type="button" value="初始化数据" onclick="Init();" /> 
<div id="Content" > 
</div> 
<div id="PagePanel" ><label id="pageInfo"></label><a href="#" onclick="InitUp()">Last</a>   <a href="#" onclick="InitNext()">Next</a></div> 
<input type="hidden" value="0" id="currPageIndex" /> 
</div> 
然后编写.js文件、实现客户端的分页控制。已经在显示页面储存了当前页码信息 一个<input type='hidden'>。 
引用js文件后,就可以用了,哈哈,很顺利。 
复制代码 代码如下:
// JScript 文件 
function Init() 
{ 
$.get("Handler.ashx", function (tablestr) { 
document.getElementById('Content').innerHTML=tablestr; 
document.getElementById('currPageIndex').value='1'; 
}); 
} 
function InitNext() 
{ 
var currIndex=document.getElementById('currPageIndex').value; 
var nextIndex=Number(currIndex)+1; 
$.get("NextHandler.ashx",{index:currIndex},function (tablestr) { 
document.getElementById('Content').innerHTML=tablestr; 
document.getElementById('pageInfo').innerText="当前第 "+nextIndex+" 页"; 
document.getElementById('currPageIndex').value=nextIndex; 
}); 
} 
function InitUp() 
{ 
var currIndex=document.getElementById('currPageIndex').value; 
var nextIndex=Number(currIndex)-1; 
$.get("PreviousHandler.ashx",{index:currIndex},function (tablestr) { 
document.getElementById('Content').innerHTML=tablestr; 
document.getElementById('pageInfo').innerText="当前第 "+nextIndex+" 页"; 
document.getElementById('currPageIndex').value=nextIndex; 
}); 
} 
将它引用到显示页面 
复制代码 代码如下:
<script type="text/javascript" src="http://www.cnblogs.com/Media/Script/jquery.js"></script> 
<script src="JScript.js" type="text/javascript"></script> 
搞定! 
剩下的就是服务端了,这个就简单了,咱就是c#代码出身,直接呼啦呼啦..... 
1、第一页初始化的数据。.... 
复制代码 代码如下:
<%@ WebHandler Language="C#" Class="Handler" %> 
using System; 
using System.Web; 
using System.Data; 
using System.Text; 
public class Handler : IHttpHandler { 
public void ProcessRequest (HttpContext context) { 
context.Response.ContentType = "text/plain"; 
DataSet ds = HebHX.DBUtility.DbHelperSQL.Query("select top 20 cust_code,cust_name,cust_addr,bank_name,bank_account from customer_info"); 
StringBuilder tb = new StringBuilder("<table class='dateGrid'><tr><th style='width:130px'>税号</th><th style='width:150px'>企业名称</th><th style='width:200px'>企业地址</th><th style='width:150px'>银行</th><th style='width:150px'>银行账号</th><tr>"); 
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
{ 
tb.Append("<tr>"); 
for (int j = 0; j < ds.Tables[0].Columns.Count; j++) 
{ 
tb.Append("<td class='Item'>"); 
tb.Append(ds.Tables[0].Rows[i][j].ToString()); 
tb.Append("</td>"); 
} 
tb.Append("</tr>"); 
} 
tb.Append("</table>"); 
context.Response.Write(tb.ToString()); 
} 
public bool IsReusable { 
get { 
return false; 
} 
} 
} 
2、点击下一页用到的 .ashx文件。 
复制代码 代码如下:
<%@ WebHandler Language="C#" Class="NextHandler" %> 
using System; 
using System.Web; 
using System.Data; 
using System.Text; 
public class NextHandler : IHttpHandler { 
public void ProcessRequest (HttpContext context) { 
context.Response.ContentType = "text/plain"; 
int pageRows = 20; 
int pageIndex = Convert.ToInt32(context.Request.Params["index"]) + 1; 
DataSet ds = HebHX.DBUtility.DbHelperSQL.Query("select top " + pageRows.ToString() + " cust_code,cust_name,cust_addr,bank_name,bank_account from customer_info where cust_id> (select max(t.cust_id) from (select top " + (pageRows * pageIndex).ToString() + " cust_id from customer_info order by cust_id) t) order by cust_id"); 
StringBuilder tb = new StringBuilder("<table class='dateGrid'><tr><th style='width:130px'>税号</th><th style='width:150px'>企业名称</th><th style='width:200px'>企业地址</th><th style='width:150px'>银行</th><th style='width:150px'>银行账号</th><tr>"); 
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
{ 
tb.Append("<tr>"); 
for (int j = 0; j < ds.Tables[0].Columns.Count; j++) 
{ 
tb.Append("<td class='Item'>"); 
tb.Append(ds.Tables[0].Rows[i][j].ToString()); 
tb.Append("</td>"); 
} 
tb.Append("</tr>"); 
} 
tb.Append("</table>"); 
context.Response.Write(tb.ToString()); 
} 
public bool IsReusable { 
get { 
return false; 
} 
} 
} 
3、点击前一页用到的.ashx文件。有思路了这个就更简单了,直接就是copy了。 
复制代码 代码如下:
<%@ WebHandler Language="C#" Class="UpHandler" %> 
using System; 
using System.Web; 
using System.Data; 
using System.Text; 
public class UpHandler : IHttpHandler { 
public void ProcessRequest (HttpContext context) { 
context.Response.ContentType = "text/plain"; 
int pageRows = 20; 
int pageIndex = Convert.ToInt32(context.Request.Params["index"]) - 1; 
DataSet ds = HebHX.DBUtility.DbHelperSQL.Query("select top " + pageRows.ToString() + " cust_code,cust_name,cust_addr,bank_name,bank_account from customer_info where cust_id> (select max(t.cust_id) from (select top " + (pageRows * pageIndex).ToString() + " cust_id from customer_info order by cust_id) t) order by cust_id"); 
StringBuilder tb = new StringBuilder("<table class='dateGrid'><tr><th style='width:130px'>税号</th><th style='width:150px'>企业名称</th><th style='width:200px'>企业地址</th><th style='width:150px'>银行</th><th style='width:150px'>银行账号</th><tr>"); 
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
{ 
tb.Append("<tr>"); 
for (int j = 0; j < ds.Tables[0].Columns.Count; j++) 
{ 
tb.Append("<td class='Item'>"); 
tb.Append(ds.Tables[0].Rows[i][j].ToString()); 
tb.Append("</td>"); 
} 
tb.Append("</tr>"); 
} 
tb.Append("</table>"); 
context.Response.Write(tb.ToString()); 
} 
public bool IsReusable { 
get { 
return false; 
} 
} 
} 
完成!直接测试..效果果然很不错,要知道我们的数据库的数据量大概在10万级别以上。..基本上感觉不到什么延时。还无刷新真是爽 啊,我要是用分页的存储过程,应该还是会有所提升的。 
效果如图、、顺便画了一幅抽象画。哈哈...顺便也欣赏一下吧。 
以上就是Asp.net中怎么利用jquery和.ashx文件实现分页,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。