Asp.net+jquery+.ashx檔案實現分頁思路

來源:互聯網
上載者:User

今天看到一個.java哥們寫過的在頁面直接請求資料列表的程式碼。它是實現選中客戶連絡人後,無重新整理的彈出div羅列其它連絡人清單的功能。忽然想到既然可以請求連絡人清單,而且無重新整理。那麼取複雜的資料列表呢,後來想到了資料分頁。我現在用了自己寫的一個分頁控制項。但是效率有時候感覺不是很高,它是以 使用者控制項+預存程序+分頁處理類 來實現分頁的。但是無可避免的就碰到了重新整理的問題即使分頁很快,但是只要這“刷”的一下總是感覺很不爽。而且還要頁面編譯一遍,還要在服務端處理ViewState。以及其它的效能損失。既然 .ashx 可以 省略頁面編譯的過程。再把分頁處理類 挪到用戶端,那應該是會效能提升不少,還沒有重新整理,一定很爽,想到就做。

我定的思路是: .ashx程式中,編寫好取得不同頁碼的程式。在頁面配置好的前提下,留下資料區域 div。然後在頁面請求 .ashx程式產生下一頁的html代碼。覆蓋div.innerHTMl 。
首先是頁面,因為是要實踐思路,所以頁面真是很簡單。引用了jquery.js 複製代碼 代碼如下:<div id="lab">
<input id="Button1" type="button" value="初始化資料" onclick="Init();" />
<div id="Content" style="width: 100%">
</div>
<div id="PagePanel" style="margin-left:20px"><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萬層級以上。..基本上感覺不到什麼延時。還無重新整理真是爽 啊,我要是用分頁的預存程序,應該還是會有所提升的。
效果、、順便畫了一幅抽象畫。哈哈...順便也欣賞一下吧。

最後還是有點疑惑,.net的ajax 的用法是不是也是這樣呢?..以前用ajax就是用一些服務端控制項,沒有真正實踐過用戶端的用法。但是我一直覺得ajax應該和現在我實現的方式大同小異。以後再學習吧..對ajax精通的哥們們可以指教一下,用戶端的ajax的 經典、實用的知識。先謝謝了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.