Handler.ashx无刷新分页

<%@ 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();
    }

}

posted @ 2011-09-02 15:51  xgcdd  阅读(764)  评论(1编辑  收藏  举报