ajax無翻頁重新整理簡單一實例2

來源:互聯網
上載者:User

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

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.