asp.net中ajax的简单应用实例代码

本文介绍了一种使用C#和jQuery实现的动态分页处理方法。通过后台获取数据并利用缓存提升效率,前端使用jQuery插件进行分页显示。具体实现了根据不同页面请求加载相应商品数据,并展示商品名称、描述等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@ 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.网页特效serializer jss = new system.web.script.serialization.javascriptserializer();
                context.response.write(jss.serialize(news));
            }

            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(jss.serialize(news));
          
            }

         }
        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; get; }
        public string description { set; get; }
        public string price { set; get; }
        public string thumbnail { set; get; }
        public string path { set; get; }

    }     


html代码//http://www.3ppt.com/Design/aspx/38279.html

    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <link href="app_themes/balloonshopdefault/balloonshop.css教程" rel="stylesheet" type="text/css" />
        <link href="pagination.css" rel="stylesheet" type="text/css" />
        <script src="新文件夹1/jscript.js" type="text/javascript">
        </script><script src="新文件夹1/jquery.pagination.js" type="text/javascript"></script>
        <script type="text/javascript">
            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) {
                   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 < datas.length; i++) {
                            var p = datas[i];
                            var li = $("<li>" + "<h3 class='producttitle'> <a href='" + p.path + "'>" + p.name + "</a></h3>" +
                                                   "<a href='" + p.path + "'>" + "<img src=" + "productimages/" + p.thumbnail + " />" + "</a>"
                                                
                                                 +p.description+"<p class='detailsection'>+ price:"+p.price+"</p></li>");
    
                                   
                                            
      
                           $("#myul").append(li);
       
       
          
      
    

     
                      
                        }

                    };


                });

                $("#pagination").pagination(data, {
                    callback: pageselectcallback,
                    prev_text: '<< 上一页',
                    next_text: '下一页 >>',
                    items_per_page: 5,
                    num_display_entries: 4,
                    current_page: pageindx,
                    num_edge_entries: 1
                });
                createlink();
            }
      
        </script>
    </head>
    <body>
    <a href="catalog.aspx"></a>
     <ul style="list-style-type:none" id="myul"></ul>
     <div id="pagination" class="digg" style="float:left"/>
    </body>
    </html>

    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <link href="app_themes/balloonshopdefault/balloonshop.css" rel="stylesheet" type="text/css" />
        <link href="pagination.css" rel="stylesheet" type="text/css" />
        <script src="新文件夹1/jscript.js" type="text/javascript">
        </script><script src="新文件夹1/jquery.pagination.js" type="text/javascript"></script>
        <script type="text/javascript">
            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) {
                   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 < datas.length; i++) {
                            var p = datas[i];
                            var li = $("<li>" + "<h3 class='producttitle'> <a href='" + p.path + "'>" + p.name + "</a></h3>" +
                                                   "<a href='" + p.path + "'>" + "<img src=" + "productimages/" + p.thumbnail + " />" + "</a>"
                                                
                                                 +p.description+"<p class='detailsection'>+ price:"+p.price+"</p></li>");
    
                                   
                                            
      
                           $("#myul").append(li);
       
       
          
      
    

     
                      
                        }

                    };


                });

    

                $("#pagination").pagination(data, {
                    callback: pageselectcallback,
                    prev_text: '<< 上一页',
                    next_text: '下一页 >>',
                    items_per_page: 5,
                    num_display_entries: 4,
                    current_page: pageindx,
                    num_edge_entries: 1
                });
                createlink();
            }
      
        </script>
    </head>
    <body>
    <a href="catalog.aspx"></a>
     <ul style="list-style-type:none" id="myul"></ul>
     <div id="pagination" class="digg" style="float:left"/>
    </body>
    </html>

转载于:https://www.cnblogs.com/huabingchen/archive/2011/03/03/1969451.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值