asp.net中怎么利用ajax實(shí)現(xiàn)分頁(yè),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)公司擁有一支富有激情的企業(yè)網(wǎng)站制作團(tuán)隊(duì),在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)深耕十年,專業(yè)且經(jīng)驗(yàn)豐富。十年網(wǎng)站優(yōu)化營(yíng)銷經(jīng)驗(yàn),我們已為上千中小企業(yè)提供了成都做網(wǎng)站、成都網(wǎng)站制作解決方案,按需網(wǎng)站制作,設(shè)計(jì)滿意,售后服務(wù)無(wú)憂。所有客戶皆提供一年免費(fèi)網(wǎng)站維護(hù)!
具體如下:
這里涉及兩個(gè).aspx文件,一個(gè)叫Default.aspx,一個(gè)叫AjaxOperations.aspx,第一個(gè)用來(lái)顯示一些測(cè)試數(shù)據(jù),后一個(gè)用來(lái)對(duì)分頁(yè)進(jìn)行處理。js文件夾下面還有一個(gè)testJs.js的文件,它就是ajax操作的核心部分。不錯(cuò),code is cheap??创a:
/*testJs.js*/ // 此函數(shù)等價(jià)于document.getElementById /document.all function $(s) { if (document.getElementById) { return eval('document.getElementById("' + s + '")'); } else { return eval('document.all.' + s); } } // 創(chuàng)建 XMLHttpRequest對(duì)象,以發(fā)送ajax請(qǐng)求 function createXMLHTTP() { var xmlHttp = false; var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; for (var i = 0; i < arrSignatures.length; i++) { try { xmlHttp = new ActiveXObject(arrSignatures[i]); return xmlHttp; } catch (oError) { xmlHttp = false; //ignore } } // throw new Error("MSXML is not installed on your system."); if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); } return xmlHttp; } //window.onload = showPages(1, 10, 100); //cP目前頁(yè)碼, tP總頁(yè)數(shù) ,tN總記錄數(shù) function showPages(cP, tP, tN) { //處理頁(yè)碼大于總頁(yè)數(shù) if (cP >= tP) { cP = tP; } //處理頁(yè)碼小于1 if (cP < 1) { cP = 1; } var trPg = $("trPager"); var newCellOne = trPg.insertCell(0); newCellOne.width = "20%"; var newCellTwo = trPg.insertCell(1); newCellTwo.width = "15%"; newCellTwo.id = "pgSummary"; var newCellThree = trPg.insertCell(2); newCellThree.width = "45%"; newCellThree.id = "pgNumContext"; var newCellFour = trPg.insertCell(3) newCellFour.width = "20%"; newCellTwo.innerHTML = "共" + tN + "項(xiàng) 第" + cP + "/" + tP + "頁(yè)"; var pageHtml = " 首頁(yè)"; pageHtml += " 上頁(yè) "; for (var i = 1; i < tP + 1; i++) { var numColor = ""; if (i == 1) numColor = "red"; pageHtml += "" + i + " "; } pageHtml += " 下頁(yè)"; pageHtml += " 尾頁(yè)"; pageHtml += " " + ""; newCellThree.innerHTML = pageHtml; } //構(gòu)造頁(yè)面跳轉(zhuǎn)的js函數(shù),函數(shù)中需要判斷輸入的頁(yè)號(hào)是否是合法的數(shù)值 function forward() { if (!(/^([-]){0,1}([0-9]){1,}$/.test($("pgNumber").value))) { //輸入的值不合法 alert("請(qǐng)輸入合法的頁(yè)號(hào)!"); $("pgNumber").focus(); $("pgNumber").select(); } else gotoPage($("pgNumber").value); } //處理在跳轉(zhuǎn)頁(yè)面上按下回車的情況 function handleEnterOnPgNumber() { if (event.keyCode == 13) { forward(); return false; } return true; } function gotoPage(oNum) { // 頁(yè)數(shù)不能為0或者為負(fù)數(shù) (首頁(yè),尾頁(yè)) if (oNum > 0) { var totalNumber = parseInt($("totalNum").innerText); //總記錄數(shù) var curPgNumber = parseInt($("cuPgNumber").innerText); var totalPgNumber = parseInt($("sumPgNumber").innerText); // 總頁(yè)數(shù) if (parseInt(oNum) <= totalPgNumber) { chgPages(oNum, totalPgNumber, totalNumber); for (var k = 1; k < totalPgNumber + 1; k++) { $("numPg" + k).style.color = ""; } $("numPg" + oNum).style.color = "red"; getPagerInfo(oNum); } else { alert("請(qǐng)輸入合法的頁(yè)號(hào)!"); $("pgNumber").focus(); $("pgNumber").select(); return; } } } function goToNextPrev(oNum) { var addNum = parseInt(oNum); var totalNumber = parseInt($("totalNum").innerText); //總記錄數(shù) var curPgNumber = parseInt($("cuPgNumber").innerText); var totalPgNumber = parseInt($("sumPgNumber").innerText); // 總頁(yè)數(shù) //如果當(dāng)前頁(yè)是第一頁(yè),點(diǎn)擊前一頁(yè)不用刷新 //如果當(dāng)前頁(yè)是最后一頁(yè),點(diǎn)擊下一頁(yè)不用刷新 if ((curPgNumber + addNum) > 0 && (curPgNumber + addNum) <= totalPgNumber) { chgPages(parseInt(curPgNumber + addNum), totalPgNumber, totalNumber); for (var k = 1; k < totalPgNumber + 1; k++) { $("numPg" + k).style.color = ""; } $("numPg" + parseInt(curPgNumber + addNum)).style.color = "red"; getPagerInfo(parseInt(curPgNumber + addNum)); } } function chgPages(cuPg, toPg, tNum) { // $("totalNum").innerHTML = tNum; $("cuPgNumber").innerHTML = cuPg; //$("sumPgNumber").innerHTML = toPg; } function getPagerInfo(oNum) { // 處理請(qǐng)求,更新內(nèi)容 var xmlReq = createXMLHTTP(); xmlReq.open("post", "/AjaxOperations.aspx?pgNumber=" + oNum, true); xmlReq.onreadystatechange = function() { if (xmlReq.readyState == 4) { if (xmlReq.status == 200) { //xmlReq.responseText為輸出的那段字符串 $("tbTest").innerHTML = xmlReq.responseText; } else { $("tbTest").innerHTML = " 獲取數(shù)據(jù)中,請(qǐng)稍等..."; //alert("Connect the server failed!"); } } } xmlReq.send(null); }
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebTest2008.Default" %>
Default.aspx.cs:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebTest2008 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } }
AjaxOperations.aspx:
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxOperations.aspx.cs" Inherits="WebTest2008.AjaxOperations" %>
AjaxOperations.aspx.cs:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebTest2008 { public partial class AjaxOperations : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(Request["pgNumber"])) { //int pgNum = Convert.ToInt32(Request["pgNumber"]); Response.Write("第" + Request["pgNumber"] + "頁(yè)"); } } } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。