No refreshing Ajax pages

Source: Internet
Author: User

I wrote a program some time ago and used Ajax without refreshing pages. I learned this video from the "Chuanzhi podcast.

Core Process: query the database and obtain the total number of pages by using "how many pieces of data are there"/"how many pieces of data are there on each page" = "how many pages are there. In this way, the page number is displayed.

Obtain a datatable (query result) by querying the actual data, serialize the data, and upload the data to the client to implement the real part of the product.

Now let's share the principles and code:

1. Two SQL statements are required (the stored procedure is better ):

(1) How many data records are returned:

Select count (*) from table

(2) return the datatable from the MTH to the nth data.

Select top (10) * from table where id not in (

Select top (20) from table where condition order by id desc)

And (condition) order by id desc // applicable to databases of various editions

2. Write a general Handler, This program will accept parameters sent from Ajax, and after processing, call the preceding two SQL statements to return the corresponding values:

(1) parameters that can be passed in (parameter: 1. page number to return; 2. how many data entries per page; 3. action), returns the corresponding data able or the total number of pages

(2) specific ideas: A. determine the action to determine the returned result (1. Return the total number of pages 2. Return the datatable of the corresponding page number)

B. Pass. calculate the total number of data entries and total number of data entries on each page.

C. Return the required data based on the number of data entries on each page and the page number of the required data.

3. The specific code is as follows: note that an error occurs when serializing the able in JSON format. You must first convert the datatable to list <>

(1) General processing program. ashx

Public class pagedservice: ihttphandler
{

Public void processrequest (httpcontext context)
{
Context. response. contenttype = "text/plain ";
// Context. response. Write ("Hello World ");

Productmanager PM;
// Check whether the returned item quantity or item information list
String action = context. request ["action"];
String tablename = context. request ["tablename"];
String type3id = context. request ["type3id"];
If (Action = "getproductcount ")
{
PM = new productmanager ();
// The number of items to return
Int I = PM. getproductcount (tablename, type3id );
Context. response. Write (I );
}
Else if (Action = "getpagedate ")
{
PM = new productmanager ();
// The List of items to return
String selectcount = context. request ["selectcount"];
String pageindex = context. request ["pageindex"];
VaR DATA = PM. getpagedate (tablename, type3id, selectcount, pageindex );
List <comment> List = new list <comment> ();
For (INT I = 0; I <data. Rows. Count; I ++)
{
VaR ROW = data. Rows [I];
String thetype3id = data. Rows [I] ["type3id"]. tostring ();
Type3manager t3m = new type3manager ();
String thetablename = t3m. gettablenamebytype3id (thetype3id). Trim ();
List. Add (New Comment ()
{
Id = row ["ID"]. tostring (),
Productname = row ["productname"]. tostring (),
ImagePath = row ["ImagePath"]. tostring (),
Discribe = row ["discribe"]. tostring (),
Price = row ["price"]. tostring (),
Tablename = thetablename
});
}
Javascriptserializer JSS = new javascriptserializer ();
Context. response. Write (JSS. serialize (list ));
}
}
Public class comment
{
Public String ID {Get; set ;}
Public String productname {Get; set ;}
Public String ImagePath {Get; set ;}
Public String discribe {Get; set ;}
Public String price {Get; set ;}
Public String tablename {Get; set;} // The added property to jump to the tag
}
Public bool isreusable
{
Get
{
Return false;
}
}
}

(2) Front-end jquery Ajax code

$. Post ("../pagedservice. ashx", {"action": "getpagedate", "selectcount": selectcount,

"Pageindex": pageindex, "tablename": tablename,

"Type3id": type3id}, function (data, status ){}

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.