惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - cjnong

EVA:自定义字段实现 Mac OS 下安装mysqlclient报“mysql_config not found”的解决 Tensorflow之训练MNIST(1) [新手]在macOS环境下安装xdebug 31.8重拾技术人生 MAC下安装MAMP后,mysql server无法启动 用myeclipse配置hibernate 重学JSP—设置CATALINA_HOME环境变量 Java 回调函数 转自:http://blog.sina.com.cn/s/blog_48cf38890100go6x.html android学习笔记 创建一个android activity 启动不了android模拟器 口语考试制作(三)——使用DllImport调用非托管函数 在线口语考试系统制作(二) 在线口语考试制作(一) 网页播放器的控制参数的收集 使用不同版本的.net框架使服务器应用程序不可用 ASP.NET生成静态页面的方法 sql2005转SQL2000出现的问题
实现页面无刷新(使用回调函数) - cjnong - 博客园
cjnong · 2007-09-03 · via 博客园 - cjnong

实现页面无刷新目前我知道有两种方法(其实都是基于XMLHTTP的)
方法一:很多AJAX的参考书上都有的方法

 1var xmlHttp; 
 2function createXmlHttp(){
 3       if(window.ActiveXObject)
 4       {
 5                   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 6       }

 7       else if(window.XMLHttpRequest)
 8       {
 9           xmlHttp = new XMLHttpRequest();
10       }
    
11  }

12
13 //按钮的单击事件触发后调用这个函数
14 function statrRequest()
15  {
16  
17      createXmlHttp();
18    var url = getUrl();//获得URL包括带有的参数
19        xmlHttp.onreadystatechange = handleStateChange;
20    xmlHttp.open("GET",url,true);
21    //xmlHttp.setRequestHeader(" Content- Type " , " application/x-www-form-urlencoded " );
22//这是POST必须的
23    xmlHttp.send(null);
24    
25    
26  }

27
28//返回时调用的函数
29 function handleStateChange()
30  {
31      if(xmlHttp.readyState==3)//当服务器还没有返回时,显示一张图片(loading)
32    {
33        
34        var oDiv = document.getElementById("loading");
35        if(oDiv.childNodes.length<=0){
36        
37        var img = document.createElement("img");
38        img.setAttribute("src","images/_loading.gif")
39        img.setAttribute("id","loading");
40    
41        }

42        else
43        {
44            var img = document.getElementById("loading");
45            img.style.display = "inline";
46        }

47                
48    }
else if(xmlHttp.readyState==4){
49        if(xmlHttp.status==200)
50        {
51            var img = document.getElementById("loading");
52            img.style.display = "none";
53            var oTable = document.getElementById("table");
54            oTable.innerHTML = xmlHttp.responseText;
55                    }

56        
57    }

本人觉得难点在于服务器返回的格式,服务器应该返回已字符串,我知道在服务器端可以接受HTML页传过来的参数,但是不知道服务器端处理后怎么返回,调用一个有返回值的函数吗?哪位打人解释一下。
下面介绍第二种方法,使用,NET的回调函数
第一,服务器端页面必须实现 System.Web.UI.ICallbackEventHandler接口,该接口有两个函数

 1  ICallbackEventHandler 成员

第一个用于返回一个字符串到客户端(通常是HTML标记和文本内容)
第二个函数当客户端调用回调函数时执行(通常用于执行逻辑代码,处理业务逻辑)
(注意MSDN回调函数的示例有误)
第二:使用JS写接受服务器返回来的结果的函数,和相应事件的函数

 1//这是响应onclick时间的函数
 2function Query()
 3  {
 4     var oType = document.getElementById("se_type");
 5     var oPrice = document.getElementById("se_price");
 6     var oRent = document.getElementById("se_rentfee");
 7     var oArea = document.getElementById("se_area");
 8     var oAcreage = document.getElementById("se_acreage");
 9     var oJzlb = document.getElementById("se_jzlb");
10     var oHouseType = document.getElementById("se_houseType");
11     var oDecorate = document.getElementById("se_decorate");
12     var oKey = document.getElementById("txt_key");
13     
14
15     
16     var value = new Array();
17     value[0]= oType.options[ oType.selectedIndex].value;
18     value[1]= oPrice.options[ oPrice.selectedIndex].value;
19     value[2]= oRent.options[ oRent.selectedIndex].value;
20     value[3]= oArea.options[ oArea.selectedIndex].text;
21     value[4= oAcreage.options[ oAcreage.selectedIndex].value;
22     value[5= oJzlb.options[ oJzlb.selectedIndex].value;
23     value[6= oHouseType.options[ oHouseType.selectedIndex].value;
24     value[7= oDecorate.options[ oDecorate.selectedIndex].value;
25     value[8= oKey.value;
26    
27    var request = value.toString();
28    
29     CallServer(request,"context");
30     
31     
32  }

33
34//这是接受服务器返回的结果,并在页面上显示的函数
35function ReceiveServerData(rValue)
36
37 
38    var oTable = document.getElementById("rTable");
39  
40    oTable.innerHTML = rValue;
41  
42 }

第三:使用Page.ClientScript.GetCallbackEventReference()方法,取得客户端函数引用

1protected void Page_Load(object sender, EventArgs e)
2    {
3        String cbReference = Page.ClientScript.GetCallbackEventReference(this"request""ReceiveServerData""context");
4        String callback = "function CallServer(request,context)" 
5            +"{" + cbReference + "}";
6        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callback, true);
7
8    }

在6行注册了一个客户端脚本,运行后,客户端出现如下代码

1<script type="text/javascript">
2<!--
3function CallServer(request,context){WebForm_DoCallback('__Page',request,ReceiveServerData,context,null,false)}// -->
4
</script>

总结:基于.NET的无刷新页面,其实是实现了 System.Web.UI.ICallbackEventHandler接口的PAGE类,使用这种方法,可以极大的提升WEB程序的性能