ajax无翻页刷新简单实例2

1,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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>微博样式1</title>
</head>
<script type="text/javascript">
  var xmlHttp;
 
  function createXMLHttpRequest()
  {
    if(window.ActiveXObject)
    {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        xmlHttp = new XMLHttpRequest();
    }
  }
 
  function addNumber()
  {
    createXMLHttpRequest();
    //var url= "AjaxMicrobloggingPaging.aspx?Num1="+document.getElementById("num1").value+"&Num2="+document.getElementById("num2").value;
    var url= "Handler.ashx";
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=showResult;
    xmlHttp.send(null);
  }
 
  function showResult()
  {
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
            document.getElementById("mlist").innerHTML=xmlHttp.responseText;
        }
     }
  }
  </script>

<body>
         <div class="content" id="mlist"></div>
     <input type="button" name="fdfds" value="计算" onclick="addNumber();" />
</body>
</html>
2,动态页面

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

using System;
using System.Web;
using System.Text;
using System.Data;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write(server_Side_Processing(5, 1));
    }
 
    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;
        DataAccess da;
        int i;


        resultXML.Append("<?xml version='1.0' encoding='gb2312'?>");
        resultXML.Append("<ajax-response>\n");
        resultXML.Append("<root>\n");

        try
        {
            if (currentPage == 1)
            {
                str_xml = "select top " + pageSize + " * from tbMicroblog order by MicroblogID desc";
            }
            else
            {
                str_xml = "select top " + pageSize + " * from tbMicroblog  MicroblogID not in (select top " + pageSize * (currentPage - 1) + " * from tbMicroblog order by MicroblogID desc) where order by MicroblogID desc";
            }
            da = new DataAccess();
            ds = da.GetDataSetResult(str_xml, "xml_table");

            if (ds != null)
            {

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

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

                    if (ds.Tables[0].Rows[i]["UserID"].ToString().Trim() != "")
                    {
                        resultXML.Append("\t<UserID>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["UserID"].ToString().Trim()) + "</UserID>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<UserID>#</UserID>\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");
                    }

                    if (ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim() != "")
                    {
                        resultXML.Append("\t<Pubdate>" + HttpUtility.UrlEncodeUnicode(ds.Tables[0].Rows[i]["Pubdate"].ToString().Trim()) + "</Pubdate>\n");
                    }
                    else
                    {
                        resultXML.Append("\t<Pubdate>#</Pubdate>\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/superfeeling/p/2782455.html