Ajax + Asp source code] Page 1/2 of the table (no framework is used) for reading database content

Source: Internet
Author: User

Function:
The table that reads the database content. the Data Reading method is ajax, And the paging method is also. the code is not well written. haha. for reference by some ajax learners. if there is any problem, please follow up and correct it. I will continually revise the code based on your suggestions.
There is still a small problem. If you have any friends interested in helping me solve it, I would be very grateful.
When you flip to the last page, if there is only one line, there will be blank and undefined. I have tried many methods and have not properly solved this problem.
Note: In some of the following, the smiling faces are uppercase d.
In fact, many places can be controlled based on the characters output in the asp file. At present, it has not been improved. For example, how many rows are displayed in a column.
Asp page code:
File Name: demo. Asp
Copy codeThe Code is as follows:
'The following Sub is the latency used to debug 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
Response. write rs (0) & "," & rs (1) & "," & rs (2) & "," & rs (3 )&","
Rs. movenext
Next

Rs. close
Set rs = nothing
%>


Page code for ajax Processing
File Name: Ajax. js
Copy codeThe Code is as follows:
Var request = false;
// ------------------ Determine the browser and create an object ----------------------------------------------------------------
Try {// try to create an XMLHttpRequest object
Request = new XMLHttpRequest (); // --- this object will be compatible with most browsers except IE!
}//-
Catch (trymicrosoft) {// If catch (trymicrosoft) fails)

Try {// try to use objects compatible with later IE versions (Msxml2.XMLHTTP)
Request = new ActiveXObject ("Msxml2.XMLHTTP"); // --- this object will be compatible with older versions of IE
}//-
Catch (othermicrosoft) {// If catch (othermicrosoft) fails)

Try {// try to use objects compatible with older IE versions (Microsoft. XMLHTTP)
Request = new ActiveXObject ("Microsoft. XMLHTTP"); // --- this object will be compatible with the new version of IE
}//-
Catch (failed) {// catch (failed) if the catch fails)
Request = false; // request = false
Alert ("Object Error! ");
}
}
}
DefaultPages ();
// Configure //-------------------------------------------------------------------------------------------------------------*/


************************** *********************************

Function defaultPages (){
Var url = "Demo. asp ";
Request. open ("GET", url, true); // open method ("method", url, asynchronous or not)
Request. onreadystatechange = updatePage; // determine the server processing result
Request. send (null); // send.
}

Function selectPages (Method, Pages ){

// Delete the previous data first
For (I = 0; I <3; 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 );
}
}

// ************ Whether the server has been processed. **************************************** **************************************** **

Function updatePage (){
If (request. readyState <= 3) {// less than or equal to status 3 ---- loading.
Document. getElementById ("Status"). innerHTML = "Loading ...";
Document. getElementById ("Pages"). innerHTML = "Previous │ Next ";
}

If (request. readyState = 4) {// status 4 ---- complete.
If (request. status = 200) {// if the http status is 200 -- when data is obtained successfully.
RequestAllright ();
} Else {// If the http status is 404 -- http reports an error.
Document. getElementById ("Status"). innerHTML = "Error:" + request. status;
Document. getElementById ("Pages"). innerHTML = "Previous │ Next ";
}
}
}

// ************ Processing completed-data generation. **************************************** **************************************** **

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; List <Contact. length; List ++ ){
Var MyTr = MyTable. insertRow (); // create a Tr tag
For (var Cell = 0; Cell <Contact [List]. length; Cell ++) {// Td Loop
Var MyTd = MyTr. insertCell (); // create a Td tag
MyTd. innerHTML = Contact [List] [Cell]; // insert content to the Td tag
If (Cell = 0) // if the current is the first Td of each row
MyTd. id = "Number"; // assign the ID to Number
}
}

// Judgment on the next page of the previous page, can I click ************************************* **************************************** *************
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>"
}
// Judgment on the next page of the previous page, can I click ************************************* **************************************** *************

Document. getElementById ("PageCount"). value = PageCount;
Document. getElementById ("PageNow"). value = PageNow;
Document. getElementById ("Status"). innerHTML = "Done! ";

// Alert (PageCount); // debug
// Alert (PageNow); // debug

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.