加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

ASP.NET中AJAX的异步加载(Demo演示)

发布时间:2020-05-10 20:11:09 所属栏目:编程 来源:站长网
导读:此次的Demo是一个页面,页面上有两行字,然后后头用AJAX,行使一个下拉框去替代第一行笔墨[/code] 第一个是被替代的网页 !DOCTYPE htmlhtmlhead meta charset="utf-8" / title/title script type="text/javascript"var xmlHttpRequest;function createXmlH

此次的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> <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演示)的文章就先容到这了,更多相干ASP.NET中AJAX异步加载内容请搜刮剧本之家早年的文章或继承赏识下面的相干文章但愿各人往后多多支持剧本之家!

(编辑:河北网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读