利用ashx實現AJAX的非同步資料調用

來源:互聯網
上載者:User

 ashx檔案,.net一般處理常式。繼承IHttpHandler介面,HttpContext實現個別HTTP請求。

實現:利用本地SQL2000預設的 northwind 資料庫

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

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

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context)
    {

        /*---------------------------自訂代碼----------------------------*/
        string str = @"server = .;uid =sa;password = sa;database = northwind";

        //取資料庫中的表

        SqlConnection Con = new SqlConnection(str);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = new SqlCommand("select * from Employees", Con);
        Con.Open();
        sda.Fill(ds);
        Con.Close();
        //將表填充到DataTable
        DataTable dt = ds.Tables[0];

        // 構造輸出資料流格式
        StringBuilder strPart = new StringBuilder();
        strPart.Append("<table>");
        for (int i = 0; i < 5; i++)
        {
            strPart.AppendFormat("<tr><td><a href ='Default.aspx?id={0}'>{1}</a></td></tr>", dt.Rows[i]["EmployeeID"],dt.Rows[i]["LastName"]);
        }
        strPart.Append("</table>");
        string s = strPart.ToString();

        //根據HTTP局部請求返迴流到頁面
        context.Response.Write(s);
       
       
    }

    public bool IsReusable
    {
        get {
            return false;
        }
    }

}

運行下這個ASHX檔案將地址複製下來

在靜態頁面調用這個ASHX檔案

<!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>

    <script type="text/jscript">
     function list(url,divList)    //ASHX檔案的地址,留輸出DIV的ID(參數)
     {
        var xmlHttp;
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            xmlHttp = new XMLHttpRequest();
        }
   
        xmlHttp.open("get",url,true);
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4)
            {
                if(xmlHttp.status == 200)
                {
                    document.getElementById(divList).innerHTML =xmlHttp.responseText;
                   
                }
            }
        }
        xmlHttp.send(null); 
    }
    </script>

</head>
<body onload ="list('http://localhost:14379/WebHtmlPartDynamic/Handler.ashx','divList')">
    <div id="divList">
    </div>
   
    <div id ="divtest">
        <input id="Button1" type="button" value="button" onclick ="list('http://localhost:14379/WebHtmlPartDynamic/Handler.ashx','divtest')"/>
    </div>
   
</body>
</html>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.