ASP.NET中AJAX的异步加载(Demo演示)
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了ASP.NET中AJAX的异步加载(Demo演示),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
此次的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 style="color:red">这里显示部门信息</p> </div> <script type="text/javascript">sendRequest()</script> <div> <p style="color:red">这里显示部门信息结束了</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异步加载内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程