asp.net ajax Simple Application Example

Source: Internet
Author: User
Tags httpcontext prev serialization xmlns

ASP tutorial. net Files

@ WebHandler language= "C #" class= "handler"%>

using System;
Using System.Web;
Using System.Collections.Generic;
Using System.Data;

public class Handler:ihttphandler


{


public void GetPage (HttpContext context)


{


int pagenum = Convert.ToInt32 (context.request["Pagenum"]); pagenum++;


if (Pagenum <= 3)


{


Dataset1.productdatatable CacheData = (dataset1.productdatatable) context.cache["CacheKey"];


if (CacheData = null)


{


CacheData = new Dataset1tableadapters.producttableadapter (). GETDATABYPRICEASC (1, 15);


Context.Cache.Insert ("CacheKey", CacheData, NULL, System.web.caching.cache.noabsoluteexpiration, Timespan.fromseconds (80));


}


List<product> news = new list<product> ();


for (int i = (Pagenum * 5)-4; I <= pagenum * 5; i++)


{


Dataset1.productrow row = cachedata[i-1];


string s = Link.toproduct (row.productid.tostring ());


News.add (new Product ()


{


Name = Row.name,


Description = Row.description,


Price = Row.price.tostring (),


thumbnail = Row.thumbnail,


Path = s


});


}
System.web.script.serialization. Web Effects Serializer JSS = new System.web.script.serialization.javascriptserializer ();
Context.Response.Write (News) (jss.serialize);
}

Else
{

var data = new Dataset1tableadapters.producttableadapter (). GETDATABYPRICEASC (Pagenum * 5)-4, Pagenum * 5);





List<product> news = new list<product> ();


for (int i =0 i <5; i++)


{


Dataset1.productrow row = Data[i];


string s = Link.toproduct (row.productid.tostring ());


News.add (new Product ()


{


Name = Row.name,


Description = Row.description,


Price = Row.price.tostring (),


thumbnail = Row.thumbnail,


Path = s


});


}
System.web.script.serialization.javascriptserializer JSS = new System.web.script.serialization.javascriptserializer ();
Context.Response.Write (News) (jss.serialize);

}

}


public void ProcessRequest (HttpContext context)


{


Context.response.contenttype = "Text/plain";


String action = context.request["Action"];


if (action = = "GetPage")


{


int allcount = new Dataset1tableadapters.producttableadapter (). GetCount (). value;


Context.Response.Write (Allcount);





}


else if (action = = "GETPAGEDATEASC")


{





GetPage ("GETPAGEDATEASC", context);


}


else if (action = = "Getpagedatedesc")


{


GetPage ("Getpagedatedesc", context);


}





}








public bool IsReusable


{


Get


{


return false;


}


}





}
public class product
{
public string name {set;
public string Description {set;
public string Price {set;
public string thumbnail {set;
public string Path {set;

}


HTML code

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">

&lt;html xmlns= "http://www.w3.org/1999/xhtml" &gt;


&lt;head&gt;


&lt;title&gt;&lt;/title&gt;


&lt;link href= "app_themes/balloonshopdefault/balloonshop.css Tutorial" rel= "stylesheet" type= "Text/css"/&gt;


&lt;link href= "Pagination.css" rel= "stylesheet" type= "Text/css"/&gt;


&lt;script src= "new Folder 1/jscript.js" Type= "Text/javascript" &gt;


&lt;/script&gt;&lt;script src= "new Folder 1/jquery.pagination.js" Type= "Text/javascript" &gt;&lt;/script&gt;


&lt;script type= "Text/javascript" &gt;


function Createlink () {


$ ("#pagination a"). each (function (i) {


if (!isnan (parseint (this.innerhtml)) {


This.href = "/balloonshop/balloons-for-children/message-balloons/page=" + parseint (this.innerhtml);


}


});


}

        var data;
        $ (function () {
             $.post ("Handler.ashx", {"Action": "GetPage"}, Function (data123, status) {
  & nbsp;            data=data123;
               initdata (0);
             
            });
          
        });

function Pageselectcallback (page_id, JQ) {
InitData (page_id);
return false;
};


function InitData (pageindx) {

$.post ("Handler.ashx", {"Action": "Getpagedate", "Pagenum": Pageindx}, function (data, status) {

if (status = = "Success") {


var datas = $.parsejson (data);


$ ("#myul"). empty ();


for (var i = 0; i &lt; datas.length; i++) {


var p = datas[i];


var li = $ ("&lt;li&gt;" + "&lt;h3 class= ' producttitle ' &gt; &lt;a href= '" + P.path + "' &gt;" + p.name + "&lt;/a&gt;&lt;/h3&" gt; "+


"&lt;a href= '" + P.path + "' &gt;" + "&lt;img src=" + "productimages/" + p.thumbnail + "/&gt;" + "&lt;/a&gt;"





+p.description+ "&lt;p class= ' detailsection ' &gt;+ Price:" +p.price+ "&lt;/p&gt;&lt;/li&gt;");














$ ("#myul"). Append (LI);
















}

};


});

$ ("#pagination"). Pagination (data, {


Callback:pageselectcallback,


Prev_text: ' &lt;&lt; prev ',


Next_text: ' next page &gt;&gt; ',


Items_per_page:5,


Num_display_entries:4,


Current_page:pageindx,


Num_edge_entries:1


});


Createlink ();


}





&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;a href= "Catalog.aspx" &gt;&lt;/a&gt;


&lt;ul style= "List-style-type:none" id= "Myul" &gt;&lt;/ul&gt;


&lt;div id= "pagination" class= "Digg" style= "Float:left"/&gt;


&lt;/body&gt;


&lt;/html&gt;

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">

&lt;html xmlns= "http://www.w3.org/1999/xhtml" &gt;


&lt;head&gt;


&lt;title&gt;&lt;/title&gt;


&lt;link href= "App_themes/balloonshopdefault/balloonshop.css" rel= "stylesheet" type= "Text/css"/&gt;


&lt;link href= "Pagination.css" rel= "stylesheet" type= "Text/css"/&gt;


&lt;script src= "new Folder 1/jscript.js" Type= "Text/javascript" &gt;


&lt;/script&gt;&lt;script src= "new Folder 1/jquery.pagination.js" Type= "Text/javascript" &gt;&lt;/script&gt;


&lt;script type= "Text/javascript" &gt;


function Createlink () {


$ ("#pagination a"). each (function (i) {


if (!isnan (parseint (this.innerhtml)) {


This.href = "/balloonshop/balloons-for-children/message-balloons/page=" + parseint (this.innerhtml);


}


});


}

        var data;
        $ (function () {
             $.post ("Handler.ashx", {"Action": "GetPage"}, Function (data123, status) {
  & nbsp;            data=data123;
               initdata (0);
             
            });
          
        });

function Pageselectcallback (page_id, JQ) {
InitData (page_id);
return false;
};


function InitData (pageindx) {

$.post ("Handler.ashx", {"Action": "Getpagedate", "Pagenum": Pageindx}, function (data, status) {

if (status = = "Success") {


var datas = $.parsejson (data);


$ ("#myul"). empty ();


for (var i = 0; i &lt; datas.length; i++) {


var p = datas[i];


var li = $ ("&lt;li&gt;" + "&lt;h3 class= ' producttitle ' &gt; &lt;a href= '" + P.path + "' &gt;" + p.name + "&lt;/a&gt;&lt;/h3&" gt; "+


"&lt;a href= '" + P.path + "' &gt;" + "&lt;img src=" + "productimages/" + p.thumbnail + "/&gt;" + "&lt;/a&gt;"





+p.description+ "&lt;p class= ' detailsection ' &gt;+ Price:" +p.price+ "&lt;/p&gt;&lt;/li&gt;");














$ ("#myul"). Append (LI);
















}

};


});

$ ("#pagination"). Pagination (data, {


Callback:pageselectcallback,


Prev_text: ' &lt;&lt; prev ',


Next_text: ' next page &gt;&gt; ',


Items_per_page:5,


Num_display_entries:4,


Current_page:pageindx,


Num_edge_entries:1


});


Createlink ();


}





&lt;/script&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;a href= "Catalog.aspx" &gt;&lt;/a&gt;


&lt;ul style= "List-style-type:none" id= "Myul" &gt;&lt;/ul&gt;


&lt;div id= "pagination" class= "Digg" style= "Float:left"/&gt;


&lt;/body&gt;


&lt;/html&gt;

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.