﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-不再回头 .net学习日记＆资料-文章分类-.Net 2.0</title><link>http://www.cnitblog.com/sfep/category/2249.html</link><description>我再也不愿听你要求 我受够了你那些自私要求</description><language>zh-cn</language><lastBuildDate>Tue, 27 Sep 2011 22:41:58 GMT</lastBuildDate><pubDate>Tue, 27 Sep 2011 22:41:58 GMT</pubDate><ttl>60</ttl><item><title>ASP.NET 2.0，无刷新页面新境界！ </title><link>http://www.cnitblog.com/sfep/articles/8149.html</link><dc:creator>不再回头</dc:creator><author>不再回头</author><pubDate>Mon, 27 Mar 2006 22:20:00 GMT</pubDate><guid>http://www.cnitblog.com/sfep/articles/8149.html</guid><wfw:comment>http://www.cnitblog.com/sfep/comments/8149.html</wfw:comment><comments>http://www.cnitblog.com/sfep/articles/8149.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/sfep/comments/commentRss/8149.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/sfep/services/trackbacks/8149.html</trackback:ping><description><![CDATA[
		<p>“无刷新页面”，只是一种不确切的效果描述（其实还有其他各种方法来实现这个效果），更确切的说法是：<strong>在页面上用JavaScript调用服务器端的一个方法，然后处理返回的数据</strong>。实现它最标准的方法当然是XMLHTTP。但是，程序员都是懒惰的家伙，每个人都希望能有更方便的方法，或者，更佳的包装。比如，<a href="http://blog.joycode.com/lostinet">Lostinet</a>的<a href="http://www.lostinet.com/files/Lostinet.Rane.rar">Rane</a>就是对XMLHTTP的一个很好的包装。<br /><br />终于，在ASP.NET 2.0里面，我们可以轻松的来做到这点了。服务器端任何实现了System.Web.UI.ICallbackEventHandler接口的控件，都可以通过RaiseCallbackEvent()方法来处理从页面上的JS脚本传递过来的请求和数据，处理后，再将结果传回给页面。这项能力的底层仍然是XMLHTTP。<br /><br />下面是一个简单的演示：<br /><br />在页面上，我们放上两个文本框和一个按钮：<br /><font color="#0000ff">＜INPUT id="txtMessage"＞<br />＜INPUT onclick="callToServer();" type="button" value="Call to Server"＞<br />Result : ＜INPUT id="txtResult" ＞</font><br /><br />当点击按钮的时候，将调用JS脚本方法callToServer()，JS脚本如下：<br /><font color="#0000ff">function callToServer()<br />{<br />    var param = document.getElementById("txtUsername").value;<br />    var context = ""; <br /><![CDATA[<% = ClientScript %>]]&gt;&nbsp;&nbsp;&nbsp; ＜% = ClientScript %＞<BR>}<BR><BR>function handleResultFromServer(result, context)<BR>{<BR>&nbsp;&nbsp;&nbsp; document.getElementById("txtResult").value = result;<BR>}</FONT><BR><BR>handleResultFromServer()方法则负责将从服务器传回的数据写到txtResult这个文本框里面。<BR><BR>再看看服务器端的代码：<BR><BR><FONT color=#0000ff>public partial class Default_aspx : System.Web.UI.ICallbackEventHandler<BR>{<BR>&nbsp;&nbsp;&nbsp; private String ClientScript<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return this.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; public string RaiseCallbackEvent(string eventArgument)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return "客户端在[" + DateTime.Now.ToString() + "]传送来 [" + eventArgument + "].";<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR></FONT><BR>我们让页面直接实现ICallbackEventHandler接口，然后接口定义的RaiseCallbackEvent()方法中将服务器的时间和传来的数据一起返回回去。<BR><BR>ClientScript属性的作用是，它调用了页面的GetCallbackEventReference()方法，获得了让客户端有能力调用服务器端方法的JS脚本，并输出到页面的callToServer()方法中，这样，点击页面按钮时，就开始执行页面上包含了调用服务器方法的的callToServer()方法。<BR><BR>注意GetCallbackEventReference()方法的参数，在参数中，我们定义了客户端的哪个变量包含了要传递给服务器，服务器方法执行后，调用客户端的哪个方法等信息。GetCallbackEventReference()的详细参看请看<A href="http://lab.msdn.microsoft.com/library/en-us/cpref/html/O_T_System_Web_UI_Page_GetCallbackEventReference.asp?frame=true">这里</A>。<BR><BR>最后，我们这个页面的执行效果就是：<BR><IMG src="http://blog.joycode.com/images/blog.joycode.com/kaneboy/368/r_ClientCallback.gif"></P></html>]]&gt;</font></p>
<img src ="http://www.cnitblog.com/sfep/aggbug/8149.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/sfep/" target="_blank">不再回头</a> 2006-03-28 06:20 <a href="http://www.cnitblog.com/sfep/articles/8149.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>