Implementation of Json-based Ajax without refreshing pagination (2)

Source: Internet
Author: User

In the previous article, I wrote about how to use jquery's ajax method to read the page number of the browser and display it on the page. In this article, I will introduce how to use json to read data and bind it to the page sharer to achieve the refreshing paging effect.

In js written on the page in the previous article, there is a BindCommentList (1); // The method for loading the first page of Data bound by default. The parameter is the current page number. The first time a page is loaded, the default value is 1, that is, the data on the first page is loaded.

Then, the BindCommentList js event is triggered when the label is clicked in the page splitter. The parameter is taken from the attribute title value of the clicked a tag, indicating the page number. The BindCommentList Event code is as follows:

// Read data function BindCommentList (page) {$. post ("/Ajax/Elec_Comment/GetData. aspx ", {pid: '<% = DotNet. framework. common. queryString. QId ("id") %> ', page: page}, function (data) {var Comments = $. parseJSON (data); $ ("# divCommentList "). empty (); for (var I = 0; I <Comments. length; I ++) {var comment = Comments [I]; var span = "<span class = \" hsk \ "> <span>" + comment. id + comment. nickName + "</span> <span> from" + comment. commentIP + "</span> <span>" + comment. commentDate + "</span> <span class = \" pl_text \ ">" + comment. commentBody + "</span>" $ ("# divCommentList "). append (span );}});}

 

The ajax request page is/Ajax/Elec_Comment/GetData. aspx: transmits the previous two parameters. The pid is the corresponding product ID (This example demonstrates the implementation of the refreshing effect of the product comment), and a page parameter, indicating the current page number. When page = 1 is loaded on the homepage, the page value uploaded after clicking the page number is read from the title attribute of tag. For details about how to implement this function, refer to the portal of the previous article.

The post method of jquery will return the request result. The data in the above Code returns a json data. It is a List <T> generic set.

$ ("# DivCommentList"). empty (); clears the content in the div of the comment list .. Then traverse the returned results. Parse the returned data using json and splice the data into html and then append it to divCommentList div. ...

Next, let's take a look at the ajax Method Request page/Ajax/Elec_Comment/GetData. aspx to serialize the queried list into a json object. The Code is as follows:

Public partial class GetData: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {int page = DotNet. framework. common. queryString. strToId (DotNet. framework. common. queryString. F ("page"); int pid = DotNet. framework. common. queryString. strToId (DotNet. framework. common. queryString. F ("pid"); GSSS. HTML. BLL. promotionZone. electronicProducts. t_ElectronicProducts_Comment bll = new GSSS. HTML. BLL. promotionZone. electronicProducts. t_ElectronicProducts_Comment (); if (page = 0) {page = 1;} DataSet data = bll. getPageData (page, pid); List <commm> list = new List <commm> (); if (data = null | data. tables. count = 0 | data. tables [0]. rows. count = 0) {Response. write ("no comment"); Response. end () ;}commm model = null; foreach (DataRow row in data. tables [0]. rows) {model = new commm () {CommentBody = row ["CommentBody"]. toString (), CommentIP = row ["CommentIP"]. toString (), CommentDate = row ["CommentDate"]. toString (), CommentRate = row ["CommentRate"]. toString (), Email = row ["Email"]. toString (), id = row ["id"]. toString (), NickName = row ["NickName"]. toString (), ProdictID = row ["ProdictID"]. toString (),}; list. add (model);} JavaScriptSerializer jss = new JavaScriptSerializer (); Response. write (jss. serialize (list); Response. end () ;}} public class commm {public string CommentBody {get; set ;}public string CommentIP {get; set ;} public string CommentDate {get; set ;} public string CommentRate {get; set;} public string Email {get; set;} public string id {get; set;} public string NickName {get; set ;} public string ProdictID {get; set ;}}

The comment contains the comment time field .. He is of the DataTime type. At first he wanted to directly read the DataTable from the database and serialize it into a Json object and return it. He found that the data could not be converted and then changed to the List <T> generic type. The returned result is serialized. I don't know how to parse data of the DataTime type. I simply return a string. Therefore, a new class commm is created. The class result is similar to the product comment entity class result, but the data not used on the page is omitted.

The retrieved able data is added to the List set through traversal .. Serialize the List set into a Json object and return it to the client. The Code is as follows:

JavaScriptSerializer jss = new JavaScriptSerializer();            Response.Write(jss.Serialize(list));            Response.End();

So far. The effect of refreshing new pages is achieved. Because it is a project we are working on, we cannot provide a Demo. I hope you will forgive me. If you have better suggestions, please submit your comments, thank you !!

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.