博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Handler.ashx无刷新分页
阅读量:7068 次
发布时间:2019-06-28

本文共 7383 字,大约阅读时间需要 24 分钟。

<%@ WebHandler Language="C#" Class="Handler" %>

using System;

using System.Web;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;

public class Handler : IHttpHandler {

   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        int ps;
        int cp;
       
        if (context.Request.QueryString["pageSize"]!=null)
        {
            ps = Convert.ToInt32(context.Request.QueryString["pageSize"]);
        }
        else
        {
            ps = 20;
        }

        if (context.Request.QueryString["currentPage"]!=null)

        {
            cp = Convert.ToInt32(context.Request.QueryString["currentPage"]);
        }
        else
        {
            cp = 1;
        }

        context.Response.Write(server_Side_Processing(ps,cp));

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    /// <summary>

    ///服务器端处理程序到数据库查询数据并生成xml档返回
    /// </summary>
    public string server_Side_Processing(int pageSize, int currentPage)
    {
        StringBuilder resultXML = new StringBuilder();
        string str_xml;
        DataSet ds;
        int i;

        resultXML.Append("<?xml version='1.0' encoding='gb2312'?>");
        resultXML.Append("<ajax-response>\n");
        resultXML.Append("<pageSize>" + ps + "</pageSize>\n");
        resultXML.Append("<currentPage>" + cp + "</currentPage>\n");
        resultXML.Append("<root>\n");

        try

        {
            if (currentPage == 1)
            {
                str_xml = "select top " + pageSize + " * from individual_microblog order by Id desc";
            }
            else
            {
                str_xml = "select top " + pageSize + " * from individual_microblog  Id not in (select top " + pageSize * (currentPage - 1) + " Id from individual_microblog order by Id desc) where order by Id desc";
            }
           
            ds = DbHelperSQL.Query(str_xml);

            if (ds != null)

            {

                for (i = 0; i < pageSize; i++)

                {
                    if (ds.Tables[0].Rows[i] == null)
                    {
                        break;
                    }

                    resultXML.Append("<data>\n");

                    resultXML.Append("\t<Id>" + ds.Tables[0].Rows[0]["Id"].ToString() + "</Id>\n");
                    resultXML.Append("\t<UserId>" + ds.Tables[0].Rows[i]["UserId"].ToString() + "</UserId>\n");
                    resultXML.Append("\t<ParentId>" + ds.Tables[0].Rows[i]["ParentId"].ToString() + "</ParentId>\n");

                    if (ds.Tables[0].Rows[i]["Content"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<Content>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Content"].ToString().Trim()) + "</Content>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<Content>#</Content>\n");
                    }

                    resultXML.Append("\t<Created>" + ds.Tables[0].Rows[i]["Created"].ToString() + "</Created>\n");

                    if (ds.Tables[0].Rows[i]["PageUrl"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<PageUrl>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["PageUrl"].ToString().Trim()) + "</PageUrl>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<PageUrl>#</PageUrl>\n");
                    }

                    if (ds.Tables[0].Rows[i]["FlashUrl"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<FlashUrl>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["FlashUrl"].ToString().Trim()) + "</FlashUrl>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<FlashUrl>#</FlashUrl>\n");
                    }

                    if (ds.Tables[0].Rows[i]["PicUrl"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<PicUrl>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["PicUrl"].ToString().Trim()) + "</PicUrl>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<PicUrl>#</PicUrl>\n");
                    }

                    if (ds.Tables[0].Rows[i]["Pic"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<Pic>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Pic"].ToString().Trim()) + "</Pic>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<Pic>#</Pic>\n");
                    }

                    resultXML.Append("\t<Type>" + ds.Tables[0].Rows[i]["Type"].ToString() + "</Type>\n");

                   
                    resultXML.Append("</data>\n");
                }
            }
            else
            {
                resultXML.Append("<data>\n");
                resultXML.Append("\t<nodata>" + "No Data !" + "</nodata>\n");
                resultXML.Append("</data>\n");
            }

        }

        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught.", e);
        }

        resultXML.Append("</root>\n");

        resultXML.Append("</ajax-response>");

        return resultXML.ToString();

    }

}

 

二次修改:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;

using System.Web;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;

public class Handler : IHttpHandler {

    private int ps;
    private int cp;
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
       
        if (context.Request.QueryString["pageSize"]!=null)
        {
            ps = Convert.ToInt32(context.Request.QueryString["pageSize"]);
        }
        else
        {
            ps = 20;
        }

        if (context.Request.QueryString["currentPage"]!=null)

        {
            cp = Convert.ToInt32(context.Request.QueryString["currentPage"]);
        }
        else
        {
            cp = 1;
        }

        context.Response.Write(server_Side_Processing(ps,cp));

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    /// <summary>

    ///服务器端处理程序到数据库查询数据并生成xml档返回
    /// </summary>
    public string server_Side_Processing(int pageSize, int currentPage)
    {
        StringBuilder resultXML = new StringBuilder();
        string str_xml;
        DataSet ds;
        int i;

        resultXML.Append("<?xml version='1.0' encoding='gb2312'?>");
        resultXML.Append("<ajax-response>\n");
        resultXML.Append("<pageSize>" + ps + "</pageSize>\n");
        resultXML.Append("<currentPage>" + cp + "</currentPage>\n");
        resultXML.Append("<root>\n");

        try

        {
            if (currentPage == 1)
            {
                str_xml = "select top " + pageSize + " * from individual_microblog order by Id desc";
            }
            else
            {
                str_xml = "select top " + pageSize + " * from individual_microblog  Id not in (select top " + pageSize * (currentPage - 1) + " Id from individual_microblog order by Id desc) where order by Id desc";
            }
           
            ds = DbHelperSQL.Query(str_xml);

            if (ds != null)

            {

                for (i = 0; i < pageSize; i++)

                {
                    if (ds.Tables[0].Rows[i] == null)
                    {
                        break;
                    }

                    resultXML.Append("<data>\n");

                    resultXML.Append("\t<Id>" + ds.Tables[0].Rows[0]["Id"].ToString() + "</Id>\n");
                    resultXML.Append("\t<UserId>" + ds.Tables[0].Rows[i]["UserId"].ToString() + "</UserId>\n");
                    resultXML.Append("\t<ParentId>" + ds.Tables[0].Rows[i]["ParentId"].ToString() + "</ParentId>\n");

                    if (ds.Tables[0].Rows[i]["Content"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<Content>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Content"].ToString().Trim()) + "</Content>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<Content>#</Content>\n");
                    }

                    resultXML.Append("\t<Created>" + ds.Tables[0].Rows[i]["Created"].ToString() + "</Created>\n");

                    if (ds.Tables[0].Rows[i]["PageUrl"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<PageUrl>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["PageUrl"].ToString().Trim()) + "</PageUrl>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<PageUrl>#</PageUrl>\n");
                    }

                    if (ds.Tables[0].Rows[i]["FlashUrl"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<FlashUrl>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["FlashUrl"].ToString().Trim()) + "</FlashUrl>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<FlashUrl>#</FlashUrl>\n");
                    }

                    if (ds.Tables[0].Rows[i]["PicUrl"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<PicUrl>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["PicUrl"].ToString().Trim()) + "</PicUrl>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<PicUrl>#</PicUrl>\n");
                    }

                    if (ds.Tables[0].Rows[i]["Pic"].ToString().Trim() != "")

                    {
                        resultXML.Append("\t<Pic>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Pic"].ToString().Trim()) + "</Pic>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<Pic>#</Pic>\n");
                    }

                    resultXML.Append("\t<Type>" + ds.Tables[0].Rows[i]["Type"].ToString() + "</Type>\n");

                   
                    resultXML.Append("</data>\n");
                }
            }
            else
            {
                resultXML.Append("<data>\n");
                resultXML.Append("\t<nodata>" + "No Data !" + "</nodata>\n");
                resultXML.Append("</data>\n");
            }

        }

        catch (Exception e)
        {
            Console.WriteLine("{0} Exception caught.", e);
        }

        resultXML.Append("</root>\n");

        resultXML.Append("</ajax-response>");

        return resultXML.ToString();

    }

}

转载于:https://www.cnblogs.com/xgcblog/archive/2011/09/02/2163878.html

你可能感兴趣的文章
安全与加密-SSL交互与握手过程 创建CA和证书管理
查看>>
CentOS 6.5 LNMP环境编译搭建
查看>>
我的友情链接
查看>>
搭建最简单ceph环境(入门)
查看>>
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers
查看>>
nagios常见的故障说明
查看>>
phpcms后台栏目列表自定义字段搜索功能
查看>>
自定义弹出框
查看>>
如何扩展ESXi虚拟机磁盘容量
查看>>
sqlserver 登录方式修改,由默认的windows账户改为用sa等sql server账户登录
查看>>
Apache+tomcat 快速部署Java环境
查看>>
获取Android控件尺寸
查看>>
强大的命令行工具wmic
查看>>
CentOS6服务管理之DNS-源码安装Bind-9.10
查看>>
出现controller取不到值的情况
查看>>
解决exe4打包出现的问题
查看>>
安装.Net Framework时安装进度总是不动怎么办
查看>>
如何卸载Exchange Server 2010
查看>>
LINUX下rootkit***侦测文档-总结版
查看>>
centos7 安装zabbix4监控系统
查看>>