本篇内容介绍了“如何操作ASP.NET中AJAX的异步加载-Demo演示”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
此次的Demo是一个页面,页面上有两行字,然后后面用AJAX,使用一个下拉框去替换第一行文字[/code]
第一个是被替换的网页
<!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();//谷歌等浏览器
}
}
//请求数据
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 >这里显示部门信息</p>
</div>
<script type="text/javascript">sendRequest()</script>
<div>
<p >这里显示部门信息结束了</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="财务部"},
new Dept(){Id=2,DeptName="研发部"},
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有三秒的延迟加载
前三秒
后三秒
“如何操作ASP.NET中AJAX的异步加载-Demo演示”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。