功能:
讀取數(shù)據(jù)庫內容的表格.讀取數(shù)據(jù)方式為ajax,分頁也是.代碼寫得不好.呵呵.供一些學習ajax的朋友參考參考.如有不妥請大家跟貼糾正.我會根據(jù)大家的提議不斷修正代碼.
目前還有一個小問題,如果有朋友有興趣幫我解決一下在下不勝感激
你翻到最后一頁的時候如果只有一行會出現(xiàn)空白和undefined.這個問題我試了很多方法一直沒有很妥當?shù)慕鉀Q.
注:下面一些地方出現(xiàn)笑臉表情的是 大寫的d
其實很多地方都可以根據(jù)asp文件中輸出的字符來控制.目前還沒有往那方面完善.比如說一列顯示多少行等.
Asp頁面的代碼:
文件名:demo.Asp
復制代碼 代碼如下:
'下面這個Sub 是延遲用于調試ajax的loading
%
Sub TimeDelaySeconds(DelaySeconds)
SecCount = 0
Sec2 = 0
While SecCount DelaySeconds + 1
Sec1 = Second(Time())
If Sec1 > Sec2 Then
Sec2 = Second(Time())
SecCount = SecCount + 1
End If
Wend
End Sub
TimeDelaySeconds(0)
%>
%
Response.Expires = -9999
Response.AddHeader "Pragma","no-cache"
Response.AddHeader "cache-ctrol","no-cache"
Response.Charset = "GB2312"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" Server.MapPath("Database.mdb")
conn.Open connstr
%>
%
set rs = server.createobject("adodb.recordset")
Sqlstr = "select * from UserData order by id"
rs.open Sqlstr,conn,1,1
rs.PageSize = 3
AllPage = rs.PageCount
Page = request.QueryString ("page")
'***********************判斷
if cint(page) = 1 then
Page = 1
elseif cint(page) >= rs.PageCount then
Page = rs.PageCount
else
intpage = cint(page)
end if
'***************************
rs.AbsolutePage=page
Response.write AllPage","rs.AbsolutePage","
for i = 1 to rs.PageSize
if rs.EOF or rs.BOF then exit for
Response.write rs(0)","rs(1)","rs(2)","rs(3)","
rs.movenext
next
rs.close
Set rs = nothing
%>
ajax處理的頁面代碼
文件名:Ajax.js
復制代碼 代碼如下:
var request = false;
//------------------判斷瀏覽器并建立對象----------------------------------------------------------------
try { //嘗試創(chuàng)建 XMLHttpRequest 對象
request = new XMLHttpRequest(); //---此對象將兼容除IE外大部分瀏覽器!
} //-
catch (trymicrosoft) { //如果失敗 catch (trymicrosoft)
try { //嘗試使用較新版IE兼容的對象(Msxml2.XMLHTTP)
request = new ActiveXObject("Msxml2.XMLHTTP"); //---此對象將兼容老版本IE
} //-
catch (othermicrosoft) { //如果失敗 catch (othermicrosoft)
try { //嘗試使用較老版IE兼容的對象(Microsoft.XMLHTTP)
request = new ActiveXObject("Microsoft.XMLHTTP"); //---此對象將兼容新版本IE
} //-
catch (failed) { //如果失敗 catch (failed)
request = false; //request = false
alert("Object Error!");
}
}
}
defaultPages();
//-------------------------------------------------------------------------------------------------------------*/
//**********讀取數(shù)據(jù)***********************************************************
function defaultPages() {
var url = "Demo.asp";
request.open("GET", url, true); //open方法 ("方式",url,是否異步)
request.onreadystatechange = updatePage; //判斷服務器處理的結果
request.send(null); //送出.
}
function selectPages(Method,Pages) {
//先刪除以前的數(shù)據(jù)
for(i=0;i3;i++)
document.getElementById("MyTable").deleteRow();
//----------------
if (Method == "Previous"){
var PageTemp = document.getElementById("PageNow").value;
var PageNow = parseInt(PageTemp)-1;
//alert (PageNow);//debug
var url = "Demo.asp?Page="+PageNow;
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
else if (Method == "Next"){
var PageTemp = document.getElementById("PageNow").value;
var PageNow = parseInt(PageTemp)+1;
//alert (PageNow);//debug
var url = "Demo.asp?Page="+PageNow;
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
}
//***********服務器是否處理完成.**********************************************************************************
function updatePage() {
if (request.readyState = 3){ //小于或等于狀態(tài)3----loading.
document.getElementById("Status").innerHTML = "Loading...";
document.getElementById("Pages").innerHTML = "Previous│Next";
}
if (request.readyState == 4){ //狀態(tài)4----完成.
if (request.status == 200) { //如果http狀態(tài)為200--當成功獲取獲取數(shù)據(jù).
requestAllright();
}else{ //如果http狀態(tài)為404--http報錯.
document.getElementById("Status").innerHTML = "Error:" + request.status;
document.getElementById("Pages").innerHTML = "Previous│Next";
}
}
}
//***********處理完成-生成數(shù)據(jù).**********************************************************************************
function requestAllright() {
var getStr = request.responseText;
var getValue = getStr.split(",");
var PageCount = getValue[0];
var PageNow = getValue[1];
var Contact = [
[getValue[2],getValue[3],getValue[4],getValue[5]],
[getValue[6],getValue[7],getValue[8],getValue[9]],
[getValue[10],getValue[11],getValue[12],getValue[13]]
];
for (var List=0;ListContact.length;List++){
var MyTr = MyTable.insertRow(); //創(chuàng)建Tr標簽
for (var Cell=0;CellContact[List].length;Cell++){ //Td循環(huán)
var MyTd = MyTr.insertCell(); //創(chuàng)建Td標簽
MyTd.innerHTML = Contact[List][Cell]; //給Td標簽插入內容
if (Cell == 0) //如果當前為每行的第一個Td
MyTd.id = "Number"; //賦予其ID為Number
}
}
//上一頁下一頁處的判斷,是否可以點擊******************************************************************************************
if (PageNow == PageCount){
document.getElementById("Pages").innerHTML = "a href=javascript:selectPages('Previous','0'); target=_self class=pagesTag>Previous/a>│Next"
}
else if (PageNow == 1){
document.getElementById("Pages").innerHTML = "Previous│a href=javascript:selectPages('Next','0'); target=_self class=pagesTag>Next/a>"
}
else if (PageNow != 1 || PageNow != PageCount){
document.getElementById("Pages").innerHTML = "a href=javascript:selectPages('Previous','0'); target=_self class=pagesTag>Previous/a>│a href=javascript:selectPages('Next','0'); target=_self class=pagesTag>Next/a>"
}
//上一頁下一頁處的判斷,是否可以點擊******************************************************************************************
document.getElementById("PageCount").value = PageCount;
document.getElementById("PageNow").value = PageNow;
document.getElementById("Status").innerHTML = "Done with!";
//alert(PageCount);//debug
//alert(PageNow);//debug
}
12下一頁閱讀全文
您可能感興趣的文章:- 用Ajax讀取XML格式的數(shù)據(jù)
- Ajax讀取數(shù)據(jù)到表格的實現(xiàn)代碼
- Ajax讀取數(shù)據(jù)之分頁顯示篇實現(xiàn)代碼
- 在Ajax中使用Flash實現(xiàn)跨域數(shù)據(jù)讀取的實現(xiàn)方法
- ajax讀取數(shù)據(jù)庫內容實現(xiàn)二級聯(lián)動下拉選擇菜單示例
- AJAX如何接收JSON數(shù)據(jù)示例介紹
- JQuery處理json與ajax返回JSON實例代碼
- jquery的ajax和getJson跨域獲取json數(shù)據(jù)的實現(xiàn)方法
- ThinkPHP中使用ajax接收json數(shù)據(jù)的方法
- 如何使用ajax讀取Json中的數(shù)據(jù)