Json Ajax without refreshing pagination

Source: Internet
Author: User
Tags comments dotnet

Json Ajax without refreshing pagination

In this article, I will go on to write a new page without refreshing the effect of reading data using json and binding with the page splitter.
// Read data based on the current page


Function BindCommentList (page ){
$. Post ("/Ajax/Elec_Comment/GetData. asp tutorial x ", {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. ...

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 tutorial 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 ();
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.