ASP.NET中AJAX的異步加載(Demo演示)
此次的Demo是一個頁面,頁面上有兩行字,然后后面用AJAX,使用一個下拉框去替換第一行文字[/code]
第一個是被替換的網(wǎng)頁
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
var xmlHttpRequest;
function createXmlHttpRequest() {
if (window.ActiveXObject) {
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");//IE瀏覽器
} else {
xmlHttpRequest = new window.XMLHttpRequest();//谷歌等瀏覽器
}
}
function sendRequest() {
createXmlHttpRequest();//獲取對象
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
document.getElementById("divContent").innerHTML = xmlHttpRequest.responseText;
}
}
};
xmlHttpRequest.open("POST", "DeptHandler.ashx", true);
xmlHttpRequest.send(null);
}
</script>
<!--<script type="text/javascript">
var xmlHttpRequest;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");//IE瀏覽器
} else {
xmlHttpRequest = new window.XMLHttpRequest();//谷歌等瀏覽器
}
}
//請求數(shù)據(jù)
function sendRequest() {
createXMLHttpRequest();
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readState == 4) {
if (xmlHttpRequest.status == 200) {
document.getElementById("divContent").innerHTML = xmlHttpRequest.responseText;
}
}
}
xmlHttpRequest.open("POST", "DeptHandler.ashx", true);
xmlHttpRequest.send(null);
}
</script>-->
</head>
<body>
<div>
<div id="divContent">
<p style="color:red">這里顯示部門信息</p>
</div>
<script type="text/javascript">sendRequest()</script>
<div>
<p style="color:red">這里顯示部門信息結(jié)束了</p>
</div>
</div>
</body>
</html>
第二個是一個類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2
{
public class Dept
{
public int Id { get; set; }
public string DeptName { get; set; }
}
}
然后是一個一般處理程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
namespace WebApplication2
{
/// <summary>
/// DeptHandler 的摘要說明
/// </summary>
public class DeptHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//這里的AJAX進行了三秒的延遲
Thread.Sleep(3000);
List<Dept> depts = new List<Dept>
{
new Dept(){Id=1,DeptName="財務(wù)部"},
new Dept(){Id=2,DeptName="研發(fā)部"},
new Dept(){Id=3,DeptName="市場部"}
};
StringBuilder sb = new StringBuilder();
sb.AppendLine("<select>");
foreach (var item in depts)
{
sb.AppendLine($"<option id = {item.Id}>{item.DeptName}</option>");
}
sb.AppendLine("</select>");
context.Response.ContentType = "text/plain";
context.Response.Write(sb);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
效果圖
AJAX有三秒的延遲加載
前三秒

后三秒

到此這篇關(guān)于ASP.NET中AJAX的異步加載(Demo演示)的文章就介紹到這了,更多相關(guān)ASP.NET中AJAX異步加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用ASP.NET一般處理程序或WebService返回JSON的實現(xiàn)代碼
今天, 將為大家說明如何在 ASP.NET 中使用一般處理程序或者 WebService 向 javascript 返回 JSON2011-10-10
ASP.NET MVC SSO單點登錄設(shè)計與實現(xiàn)代碼
本篇文章主要介紹了ASP.NET MVC SSO單點登錄設(shè)計與實現(xiàn),具有一定的參考價值,有興趣的可以了解一下。2017-01-01
asp.net實現(xiàn)識別客戶端瀏覽器或操作系統(tǒng)
這里給大家匯總了使用asp.net實現(xiàn)識別客戶端瀏覽器或操作系統(tǒng)的方法和示例代碼,有需要的小伙伴可以參考下。2015-10-10
如何對ASP.NET網(wǎng)站實現(xiàn)靜態(tài)化
對于訪問量比較大的網(wǎng)站,網(wǎng)頁靜態(tài)化是一個比較可靠的解決方案。靜態(tài)化將顯著降低服務(wù)器的壓力,提升服務(wù)器處理能力。下面將介紹兩種不同的實現(xiàn)方法,并進行對比。2015-09-09
MVC+EasyUI+三層新聞網(wǎng)站建立 建站準備工作(一)
這篇文章主要為大家詳細介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第一篇,建站的準備工作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07

